Converting a custom type to string in Go

huangapple go评论130阅读模式
英文:

Converting a custom type to string in Go

问题

在这个奇怪的例子中,有人创建了一个新的类型,实际上只是一个字符串:

  1. type CustomType string
  2. const (
  3. Foobar CustomType = "somestring"
  4. )
  5. func SomeFunction() string {
  6. return string(Foobar)
  7. }

然而,这段代码无法编译:

无法将Foobar(类型为CustomType)用作返回参数中的字符串类型

你要如何修改SomeFunction,使其能够返回Foobar的字符串值("somestring")?

英文:

In this bizarre example, someone has created a new type which is really just a string:

  1. type CustomType string
  2. const (
  3. Foobar CustomType = "somestring"
  4. )
  5. func SomeFunction() string {
  6. return Foobar
  7. }

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

将该值转换为字符串:

  1. func SomeFunction() string {
  2. return string(Foobar)
  3. }

将其转换为字符串:

  1. func SomeFunction() string {
  2. return strconv.Itoa(int(Foobar))
  3. }

请注意,这里使用了strconv.Itoa函数来将整数类型的Foobar转换为字符串类型。

英文:

Convert the value to a string:

  1. func SomeFunction() string {
  2. return string(Foobar)
  3. }

答案2

得分: 27

最好为CustomType定义一个String函数-随着时间的推移,它可以让你的生活更轻松-你可以更好地控制事物,如果结构发生变化的话。如果你真的需要SomeFunction,那么让它返回Foobar.String()

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type CustomType string
  6. const (
  7. Foobar CustomType = "somestring"
  8. )
  9. func main() {
  10. fmt.Println("Hello, playground", Foobar)
  11. fmt.Printf("%s", Foobar)
  12. fmt.Println("\n\n")
  13. fmt.Println(SomeFunction())
  14. }
  15. func (c CustomType) String() string {
  16. fmt.Println("Executing String() for CustomType!")
  17. return string(c)
  18. }
  19. func SomeFunction() string {
  20. return Foobar.String()
  21. }

点击此处查看代码

英文:

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()

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type CustomType string
  6. const (
  7. Foobar CustomType = "somestring"
  8. )
  9. func main() {
  10. fmt.Println("Hello, playground", Foobar)
  11. fmt.Printf("%s", Foobar)
  12. fmt.Println("\n\n")
  13. fmt.Println(SomeFunction())
  14. }
  15. func (c CustomType) String() string {
  16. fmt.Println("Executing String() for CustomType!")
  17. return string(c)
  18. }
  19. func SomeFunction() string {
  20. return Foobar.String()
  21. }

https://play.golang.org/p/jMKMcQjQj3

答案3

得分: 10

对于每种类型T,都有一个相应的转换操作T(x),将值x转换为类型T。如果两种类型具有相同的基础类型,或者都是指向相同基础类型变量的未命名指针类型,则允许从一种类型转换为另一种类型;这些转换改变了类型但不改变值的表示。如果x可以赋值给T,则允许进行转换,但通常是多余的。

根据您的示例,以下是一些不同的示例,将返回值。

  1. package main
  2. import "fmt"
  3. type CustomType string
  4. const (
  5. Foobar CustomType = "somestring"
  6. )
  7. func SomeFunction() CustomType {
  8. return Foobar
  9. }
  10. func SomeOtherFunction() string {
  11. return string(Foobar)
  12. }
  13. func SomeOtherFunction2() CustomType {
  14. return CustomType("somestring") // 这里的值是一个静态字符串。
  15. }
  16. func main() {
  17. fmt.Println(SomeFunction())
  18. fmt.Println(SomeOtherFunction())
  19. fmt.Println(SomeOtherFunction2())
  20. }

它将输出:

  1. somestring
  2. somestring
  3. somestring

Go Playground链接

英文:

> 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.

  1. package main
  2. import "fmt"
  3. type CustomType string
  4. const (
  5. Foobar CustomType = "somestring"
  6. )
  7. func SomeFunction() CustomType {
  8. return Foobar
  9. }
  10. func SomeOtherFunction() string {
  11. return string(Foobar)
  12. }
  13. func SomeOtherFunction2() CustomType {
  14. return CustomType("somestring") // Here value is a static string.
  15. }
  16. func main() {
  17. fmt.Println(SomeFunction())
  18. fmt.Println(SomeOtherFunction())
  19. fmt.Println(SomeOtherFunction2())
  20. }

It will output:

  1. somestring
  2. somestring
  3. somestring

The Go Playground link

答案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)

huangapple
  • 本文由 发表于 2017年8月26日 11:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/45891600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定