在这个奇怪的例子中,有人创建了一个新类型,它实际上只是一个字符串:
type CustomType string
const (
Foobar CustomType = "somestring"
)
func SomeFunction() string {
return Foobar
}
但是,此代码无法编译:
不能在返回参数中使用 Foobar(类型 CustomType)作为类型字符串
您将如何修复 SomeFunction 以便它能够返回 Foobar (“somestring”) 的字符串值?
原文由 DaveUK 发布,翻译遵循 CC BY-SA 4.0 许可协议
将 值转换为字符串: