在Go中将自定义类型转换为字符串
问题描述:
在这个奇怪的示例中,有人创建了一个实际上只是字符串的新类型:
In this bizarre example, someone has created a new type which is really just a string:
type CustomType string
const (
Foobar CustomType = "somestring"
)
func SomeFunction() string {
return Foobar
}
但是,此代码无法编译:
However, this code fails to compile:
不能在返回参数中使用Foobar(CustomType类型)作为类型字符串
cannot use Foobar (type CustomType) as type string in return argument
如何修复SomeFunction,使其能够返回Foobar的字符串值("somestring")?
How would you fix SomeFunction so that it is able to return the string value of Foobar ("somestring") ?