如何在Go映射中分配结构字段

This question already has an answer here:

I want to assign field of struct which is in a map like this:

package main

import (
    "fmt"
)

type Task struct {
    Cmd string
    Desc string
}

var taskMap = map[string] Task{
    "showDir": Task{
        Cmd: "ls",
    },
    "showDisk": Task{
        Cmd: "df",
    },
}

var task = Task{
    Cmd: "ls",
}

func main() {
    // *Error*cannot assign to taskMap["showDir"].Desc
    taskMap["showDir"].Desc = "show dirs" 
    task.Desc = "show dirs" // this is ok.
    fmt.Printf("%s", taskMap)
    fmt.Printf("%s", task)
}

I can assign the Desc field in a variable task but not in a wrapped map taskMap, what has been wrong?

</div>

You can use pointers:

var taskMap = map[string]*Task{
    "showDir": {
        Cmd: "ls",
    },
    "showDisk": {
        Cmd: "df",
    },
}

func main() {
    taskMap["showDir"].Desc = "show dirs"
    fmt.Printf("%+v", taskMap["showDir"])
}

playground