为了账号安全,请及时绑定邮箱和手机立即绑定

Golang:尝试删除地图中的项目时编译错误

Golang:尝试删除地图中的项目时编译错误

Go
森林海 2022-10-10 10:31:08
我正在尝试从 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贡献1828条经验 获得超4个赞

内置函数在包中delete被遮蔽。func delete(*config, *session.Session) {}重命名包中的函数。



查看完整回答
反对 回复 2022-10-10
?
红颜莎娜

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



查看完整回答
反对 回复 2022-10-10
  • 2 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信