我正在尝试从 Golang 的地图中删除一个项目。这是代码的粗略想法:import ( "github.com/aws/aws-sdk-go/aws/session")var sessions map[string]*session.Sessiontype 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我不明白这里的问题。唯一让我怀疑的是在映射中使用指针作为类型。假设我需要保留指针类型,关于如何解决的任何想法?
2 回答
红颜莎娜
TA贡献1842条经验 获得超12个赞
你在哪里定义了一个delete函数?
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
- 2 回答
- 0 关注
- 85 浏览
添加回答
举报
0/150
提交
取消