我正在嘗試從 Golang 的地圖中刪除一個(gè)項(xiàng)目。這是代碼的粗略想法: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) }}但是我收到了這個(gè)編譯錯(cuò)誤:# ./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我不明白這里的問(wèn)題。唯一讓我懷疑的是在映射中使用指針作為類型。假設(shè)我需要保留指針類型,關(guān)于如何解決的任何想法?
2 回答

明月笑刀無(wú)情
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
內(nèi)置函數(shù)在包中delete
被遮蔽。func delete(*config, *session.Session) {}
重命名包中的函數(shù)。

紅顏莎娜
TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
你在哪里定義了一個(gè)delete函數(shù)?
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 關(guān)注
- 135 瀏覽
添加回答
舉報(bào)
0/150
提交
取消