One of the most common uses for if ..; .. { } for me has been map access.
vardata=map[string]string{}funcaddToMap(keystring,valuestring){if_,ok:=data[key];ok{fmt.Printf("%s already in map",key)return}data[key]=value}
Alternatively use !ok if you're interested in when it's NOT in the map. For anyone not familiar with _ it basically means "throw this away", i.e. in that if block I don't care about the stored value.
You are right - this is another very common use case and you can almost always use the initialization statement since your maps shouldn't typically have names like omgThisMapHasASuperLooongNameThatIsSoAnnoyingToType 😀
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One of the most common uses for
if ..; .. { }
for me has been map access.Alternatively use
!ok
if you're interested in when it's NOT in the map. For anyone not familiar with_
it basically means "throw this away", i.e. in thatif
block I don't care about the stored value.You are right - this is another very common use case and you can almost always use the initialization statement since your maps shouldn't typically have names like
omgThisMapHasASuperLooongNameThatIsSoAnnoyingToType
😀