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

Time:10-21

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?

CodePudding user response:

The built in delete function is shadowed by func delete(*config, *session.Session) {} in the package. Rename the function in the package.

CodePudding user response:

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
  •  Tags:  
  • go
  • Related