golang nil map で panic
よくやらかすやつ
var testMap map[int64]string testMap[100] = "あいうえお" testMap[101] = "かきくけこ" // panic: assignment to entry in nil map
なぜ?
初期化(メモリの確保)をしていないから
言われれば当然…
解決策
マップリテラルで初期化
2行になるから自分はやらない。
var testMap map[int64]string testMap = map[int64]string{} testMap[100] = "あいうえお" testMap[101] = "かきくけこ" fmt.Println(testMap) // map[100:あいうえお 101:かきくけこ]
makeで初期化
こっちの方が馴染みある
testMap := make(map[int64]string) // testMap = map[int64]string{} testMap[100] = "あいうえお" testMap[101] = "かきくけこ" fmt.Println(testMap) // map[100:あいうえお 101:かきくけこ]
make の方は第2引数にキャパシティを指定できる。指定しない場合は適当なキャパシティが自動で確保される。
これはマップリテラルでも同様らしい。
makeの説明にはこう書いている
// The make built-in function allocates and initializes an object of type // slice, map, or chan (only). Like new, the first argument is a type, not a // value. Unlike new, make's return type is the same as the type of its // argument, not a pointer to it. The specification of the result depends on // the type: // Slice: The size specifies the length. The capacity of the slice is // equal to its length. A second integer argument may be provided to // specify a different capacity; it must be no smaller than the // length. For example, make([]int, 0, 10) allocates an underlying array // of size 10 and returns a slice of length 0 and capacity 10 that is // backed by this underlying array. // Map: An empty map is allocated with enough space to hold the // specified number of elements. The size may be omitted, in which case // a small starting size is allocated. // Channel: The channel's buffer is initialized with the specified // buffer capacity. If zero, or the size is omitted, the channel is // unbuffered. func make(t Type, size ...IntegerType) Type ...
英語読めないのでDeepL変換。
make組み込み関数は、slice、map、chan(のみ)のいずれかの型のオブジェクトを割り当て、初期化します。newと同様に、第1引数は値ではなく型です。newとは異なり、makeの戻り値の型は引数の型と同じで、そのポインタではありません。
The make built-in function allocates and initializes an object of type slice, map, or chan (only). Like new, the first argument is a type, not a value. Unlike new, make's return type is the same as the type of its argument, not a pointer to it.
マップ: 空のマップは、指定された数の要素を格納するのに十分なスペースが割り当てられます。サイズを省略することもでき、その場合は小さなスタートサイズが割り当てられます。
Map: An empty map is allocated with enough space to hold the specified number of elements. The size may be omitted, in which case a small starting size is allocated.
"適当なキャパシティ"というか最小のキャパシティが割り当てられるっぽい。