I am using the official/unofficial language package and have this:
package main
import (
"log"
"golang.org/x/text/language"
)
func main() {
reg, _ := language.Tag{}.Region()
log.Println(reg.Canonicalize()) // US
}
I was expecting "ZZ". Why is it "US"? I can't find anything in the documentation.
If you look at the source code for the method tag{}.Region() you'll see that if region is not specified it tries to add tags and sets the likelihood to low. addTags() If you look at the implementation for addTags() you'll see
if t.lang == 0 {
t.lang = _en // default language
}
where the default language is set to English. Cheers.
If tag's language is not defined (zero by default), then it just get the first value from likelyLang
array.
x := likelyLang[t.lang]
...
if x.region != 0 {
t.setUndefinedScript(scriptID(x.script))
t.setUndefinedRegion(regionID(x.region))
}
And it has _US (0x134) region code (surprise). After that it copies region to the input tag.
P.S. @reticentroot's answer is about the same. Didn't see it during investigation.