如何使用map [string] * string

I'm trying to use sarama (Admin mode) to create a topic. Without the ConfigEntries works fine. But I need to define some configs.

I set up the topic config (Here is happening the error):

    tConfigs := map[string]*string{
        "cleanup.policy":      "delete",
        "delete.retention.ms": "36000000",
    }

But then I get an error:

./main.go:99:28: cannot use "delete" (type string) as type *string in map value
./main.go:100:28: cannot use "36000000" (type string) as type *string in map value

I'm trying to use the admin mode like this:

err = admin.CreateTopic(t.Name, &sarama.TopicDetail{
    NumPartitions:     1,
    ReplicationFactor: 3,
    ConfigEntries:     tConfigs,
}, false)

Here is the line from the sarama module that defines CreateTopic() https://github.com/Shopify/sarama/blob/master/admin.go#L18

Basically, I didn't understand how the map of pointers strings works :)

To initialize a map having string pointer value type with a composite literal, you have to use string pointer values. A string literal is not a pointer, it's just a string value.

An easy way to get a pointer to a string value is to take the address of a variable of string type, e.g.:

s1 := "delete"
s2 := "36000000"

tConfigs := map[string]*string{
    "cleanup.policy":      &s1,
    "delete.retention.ms": &s2,
}

To make it convenient when used many times, create a helper function:

func strptr(s string) *string { return &s }

And using it:

tConfigs := map[string]*string{
    "cleanup.policy":      strptr("delete"),
    "delete.retention.ms": strptr("36000000"),
}

Try the examples on the Go Playground.

See background and other options here: How do I do a literal *int64 in Go?