type Test string 的意义是什么

yangAdmin · · 722 次点击
1.给string类实现方法,用于实现某些interface 2.用于作为特殊类型,用于类型判定的地方,比如map /context的key
#1
更多评论
第一点看懂了,但第二点没有。 可以举个例子吗
#2
以context包为例 https://golang.org/pkg/context/ The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using context. Users of WithValue should define their own types for keys. To avoid allocating when assigning to an interface{}, context keys often have concrete type struct{}. Alternatively, exported context key variables' static type should be a pointer or interface. 总的来说 作为key判断的话 "key"和Test("key") 是不同的值
#3