英文:
Golang: compile error when trying to delete an item in map
问题
我正在尝试从Golang的map中删除一个项目。
以下是代码的大致思路:
import (
"github.com/aws/aws-sdk-go/aws/session"
)
var sessions map[string]*session.Session
type config struct {
...
endPoint string
...
}
func newConfig() config {
var Config config = config{endPoint: "myEndpoint"}
return Config
}
func createSession(Config *config) error {
...
sessions = make(map[string]*session.Session)
...
session, err := session.NewSession(<session info here>)
sessions[Config.endPoint] = session
...
}
func main() {
Config := newConfig()
err := createSession()
...
if <some condition where i want to delete the session> {
delete(sessions, Config.endPoint)
}
}
然而,我遇到了这个编译错误:
# ./build_my_program.sh
./myprogram.go:9998:12: cannot use sessions (type map[string]*session.Session) as type *config in argument to delete
./myprogram.go:9999:29: cannot use Config.endPoint (type string) as type *session.Session in argument to delete
我不明白这里的问题。
唯一让我怀疑的是在map中使用指针作为类型。假设我需要保持指针类型,有没有解决方法?
英文:
I'm trying to delete an item from a map in Golang.
Here's a rough idea of the code:
import (
"github.com/aws/aws-sdk-go/aws/session"
)
var sessions map[string]*session.Session
type config struct {
...
endPoint string
...
}
func newConfig() config {
var Config config = config{endPoint: "myEndpoint"}
return Config
}
func createSession(Config *config) error {
...
sessions = make(map[string]*session.Session)
...
session, err := session.NewSession( <session info here> )
sessions[Config.endPoint] = session
...
}
func main() {
Config := newConfig()
err := createSession()
...
if <some condition where i want to delete the session> {
delete(sessions, Config.endPoint)
}
}
However I'm getting this compile error:
# ./build_my_program.sh
./myprogram.go:9998:12: cannot use sessions (type map[string]*session.Session) as type *config in argument to delete
./myprogram.go:9999:29: cannot use Config.endPoint (type string) as type *session.Session in argument to delete
I don't understand the problem here.
The only thing which makes me suspicious here is the use of pointers as a type in the map. Supposing I need to keep the pointers type, any idea on how to resolve?
答案1
得分: 1
内置的 delete
函数被包中的 func delete(*config, *session.Session) {}
遮蔽了。请将包中的函数重命名。
英文:
The built in delete
function is shadowed by func delete(*config, *session.Session) {}
in the package. Rename the function in the package.
答案2
得分: 0
你在哪里声明了一个 delete
函数?
package main
func delete(a, b string) {
}
func main() {
m := map[int]int{}
m[1] = 1
delete(m, 1)
}
./del.go:9:8: 无法将 m (类型为 map[int]int) 作为 delete 函数的字符串参数使用
./del.go:9:12: 无法将 1 (类型为未指定类型的整数) 作为 delete 函数的字符串参数使用
英文:
Where did you delcared a delete
func ?
package main
func delete(a, b string) {
}
func main() {
m := map[int]int{}
m[1] = 1
delete(m, 1)
}
./del.go:9:8: cannot use m (type map[int]int) as type string in argument to delete
./del.go:9:12: cannot use 1 (type untyped int) as type string in argument to delete
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论