I need to use a map
for keys only, I don't need to store values. So I declared a map like this:
modified_accounts:=make(map[int]struct{})
The idea is to use empty struct because it doesn't consume storage.
However, when I tried to add an entry to the map,
modified_accounts[2332]=struct{}
I got a compile error:
./blockchain.go:291:28: type struct {} is not an expression
How do I add an empty key and no value to the map ?
You can do it declaring a empty variable
var Empty struct{}
func foo() {
modified_accounts := make(map[int]struct{})
modified_accounts[2332] = Empty
fmt.Println(modified_accounts)
}
Or creating a new struct every time
func bar() {
modified_accounts := make(map[int]struct{})
modified_accounts[2332] = struct{}{}
fmt.Println(modified_accounts)
}
To create a empty struct
you should use struct{}{}