Golang:尝试删除映射中的项时出现编译错误。

huangapple go评论106阅读模式
英文:

Golang: compile error when trying to delete an item in map

问题

我正在尝试从Golang的map中删除一个项目。

以下是代码的大致思路:

  1. import (
  2. "github.com/aws/aws-sdk-go/aws/session"
  3. )
  4. var sessions map[string]*session.Session
  5. type config struct {
  6. ...
  7. endPoint string
  8. ...
  9. }
  10. func newConfig() config {
  11. var Config config = config{endPoint: "myEndpoint"}
  12. return Config
  13. }
  14. func createSession(Config *config) error {
  15. ...
  16. sessions = make(map[string]*session.Session)
  17. ...
  18. session, err := session.NewSession(<session info here>)
  19. sessions[Config.endPoint] = session
  20. ...
  21. }
  22. func main() {
  23. Config := newConfig()
  24. err := createSession()
  25. ...
  26. if <some condition where i want to delete the session> {
  27. delete(sessions, Config.endPoint)
  28. }
  29. }

然而,我遇到了这个编译错误:

  1. # ./build_my_program.sh
  2. ./myprogram.go:9998:12: cannot use sessions (type map[string]*session.Session) as type *config in argument to delete
  3. ./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:

  1. import (
  2. &quot;github.com/aws/aws-sdk-go/aws/session&quot;
  3. )
  4. var sessions map[string]*session.Session
  5. type config struct {
  6. ...
  7. endPoint string
  8. ...
  9. }
  10. func newConfig() config {
  11. var Config config = config{endPoint: &quot;myEndpoint&quot;}
  12. return Config
  13. }
  14. func createSession(Config *config) error {
  15. ...
  16. sessions = make(map[string]*session.Session)
  17. ...
  18. session, err := session.NewSession( &lt;session info here&gt; )
  19. sessions[Config.endPoint] = session
  20. ...
  21. }
  22. func main() {
  23. Config := newConfig()
  24. err := createSession()
  25. ...
  26. if &lt;some condition where i want to delete the session&gt; {
  27. delete(sessions, Config.endPoint)
  28. }
  29. }

However I'm getting this compile error:

  1. # ./build_my_program.sh
  2. ./myprogram.go:9998:12: cannot use sessions (type map[string]*session.Session) as type *config in argument to delete
  3. ./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 函数?

  1. package main
  2. func delete(a, b string) {
  3. }
  4. func main() {
  5. m := map[int]int{}
  6. m[1] = 1
  7. delete(m, 1)
  8. }
  1. ./del.go:9:8: 无法将 m (类型为 map[int]int) 作为 delete 函数的字符串参数使用
  2. ./del.go:9:12: 无法将 1 (类型为未指定类型的整数) 作为 delete 函数的字符串参数使用
英文:

Where did you delcared a delete func ?

  1. package main
  2. func delete(a, b string) {
  3. }
  4. func main() {
  5. m := map[int]int{}
  6. m[1] = 1
  7. delete(m, 1)
  8. }
  1. ./del.go:9:8: cannot use m (type map[int]int) as type string in argument to delete
  2. ./del.go:9:12: cannot use 1 (type untyped int) as type string in argument to delete

huangapple
  • 本文由 发表于 2021年10月21日 08:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/69654508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定