英文:
Converting a custom type to string in Go
问题
在这个奇怪的例子中,有人创建了一个新的类型,实际上只是一个字符串:
type CustomType string
const (
Foobar CustomType = "somestring"
)
func SomeFunction() string {
return string(Foobar)
}
然而,这段代码无法编译:
无法将Foobar(类型为CustomType)用作返回参数中的字符串类型
你要如何修改SomeFunction,使其能够返回Foobar的字符串值("somestring")?
英文:
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:
> cannot use Foobar (type CustomType) as type string in return argument
How would you fix SomeFunction so that it is able to return the string value of Foobar ("somestring") ?
答案1
得分: 68
将该值转换为字符串:
func SomeFunction() string {
return string(Foobar)
}
将其转换为字符串:
func SomeFunction() string {
return strconv.Itoa(int(Foobar))
}
请注意,这里使用了strconv.Itoa
函数来将整数类型的Foobar
转换为字符串类型。
答案2
得分: 27
最好为CustomType
定义一个String
函数-随着时间的推移,它可以让你的生活更轻松-你可以更好地控制事物,如果结构发生变化的话。如果你真的需要SomeFunction
,那么让它返回Foobar.String()
。
package main
import (
"fmt"
)
type CustomType string
const (
Foobar CustomType = "somestring"
)
func main() {
fmt.Println("Hello, playground", Foobar)
fmt.Printf("%s", Foobar)
fmt.Println("\n\n")
fmt.Println(SomeFunction())
}
func (c CustomType) String() string {
fmt.Println("Executing String() for CustomType!")
return string(c)
}
func SomeFunction() string {
return Foobar.String()
}
英文:
Better to define a String
function for the Customtype
- it can make your life easier over time - you have better control over things as and if the structure evolves. If you really need SomeFunction
then let it return Foobar.String()
package main
import (
"fmt"
)
type CustomType string
const (
Foobar CustomType = "somestring"
)
func main() {
fmt.Println("Hello, playground", Foobar)
fmt.Printf("%s", Foobar)
fmt.Println("\n\n")
fmt.Println(SomeFunction())
}
func (c CustomType) String() string {
fmt.Println("Executing String() for CustomType!")
return string(c)
}
func SomeFunction() string {
return Foobar.String()
}
答案3
得分: 10
对于每种类型T,都有一个相应的转换操作T(x),将值x转换为类型T。如果两种类型具有相同的基础类型,或者都是指向相同基础类型变量的未命名指针类型,则允许从一种类型转换为另一种类型;这些转换改变了类型但不改变值的表示。如果x可以赋值给T,则允许进行转换,但通常是多余的。
根据您的示例,以下是一些不同的示例,将返回值。
package main
import "fmt"
type CustomType string
const (
Foobar CustomType = "somestring"
)
func SomeFunction() CustomType {
return Foobar
}
func SomeOtherFunction() string {
return string(Foobar)
}
func SomeOtherFunction2() CustomType {
return CustomType("somestring") // 这里的值是一个静态字符串。
}
func main() {
fmt.Println(SomeFunction())
fmt.Println(SomeOtherFunction())
fmt.Println(SomeOtherFunction2())
}
它将输出:
somestring
somestring
somestring
英文:
> For every type T, there is a corresponding conversion operation T(x)
> that converts the value x to type T. A conversion from one type to
> another is allowed if both have the same underlying type, or if both
> are unnamed pointer types that point to variables of the same
> underlying type; these conversions change the type but not the
> representation of the value. If x is assignable to T, a conversion
> is permitted but is usually redundant. - Taken from The Go
> Programming Language - by Alan A. A. Donovan
As per your example here are some of the different examples which will return the value.
package main
import "fmt"
type CustomType string
const (
Foobar CustomType = "somestring"
)
func SomeFunction() CustomType {
return Foobar
}
func SomeOtherFunction() string {
return string(Foobar)
}
func SomeOtherFunction2() CustomType {
return CustomType("somestring") // Here value is a static string.
}
func main() {
fmt.Println(SomeFunction())
fmt.Println(SomeOtherFunction())
fmt.Println(SomeOtherFunction2())
}
It will output:
somestring
somestring
somestring
答案4
得分: 1
你可以这样进行转换:
var i int = 42 var f float64 = float64(i)
在这里查看链接
你可以这样返回:
return string(Foobar)
英文:
You can convert like this:
var i int = 42
var f float64 = float64(i)
check here
you can return like this:
return string(Foobar)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论