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?