Devaraj
(Devaraj Gowda)
1
Complete Code : https://play.golang.org/p/bctvsy7ARqS
package main
import (
“fmt”
)
type Plan struct {
BufferMeasures map[string]*ItemSiteMeasure
}
type ItemSiteMeasure struct {
itemtest string
}
func main() {
fmt.Println("start")
var buff Plan
buff.AddBuffer()
fmt.Println("end")
}
func (p *Plan) AddBuffer() {
fmt.Println(“method start”)
p.BufferMeasures[“itmsit1”] = &ItemSiteMeasure{itemtest: “item1”}
fmt.Println("obj values : ", p.BufferMeasures)
fmt.Println(“method end”)
}
Error /output:
start
method start
panic: assignment to entry in nil map
goroutine 1 [running]:
main.(*Plan).AddBuffer(0xc000068f50)
/tmp/sandbox633108119/prog.go:29 +0xe8
main.main()
/tmp/sandbox633108119/prog.go:20 +0x90
petrus
(petrus)
2
To initialize a map, use the built in make
function
The Go Blog: Go maps in action
package main
import (
"fmt"
)
type Plan struct {
BufferMeasures map[string]*ItemSiteMeasure
}
type ItemSiteMeasure struct {
itemtest string
}
func main() {
fmt.Println("start")
var buff Plan
buff.AddBuffer()
fmt.Println("end")
}
func (p *Plan) AddBuffer() {
if p.BufferMeasures == nil {
p.BufferMeasures = make(map[string]*ItemSiteMeasure)
}
fmt.Println("method start")
p.BufferMeasures["itmsit1"] = &ItemSiteMeasure{itemtest: "item1"}
fmt.Println("obj values : ", p.BufferMeasures)
fmt.Println("method end")
}
.
start
method start
obj values : map[itmsit1:0xc000010230]
method end
end
Devaraj
(Devaraj Gowda)
3
Thanks Petrus, issue resolved.
system
(system)
Closed
4
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.