douxing6434
2017-08-26 03:15
浏览 528
已采纳

在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:

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") ?

图片转代码服务由CSDN问答提供 功能建议

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

 类型CustomType字符串
 
const(
 Foobar CustomType =“ somestring” 
)
 
func SomeFunction()字符串{
返回Foobar 
} 
  <  / pre> 
 
 

但是,此代码无法编译:

不能在返回参数中使用Foobar(类型CustomType)作为类型字符串

您将如何修复SomeFunction,使其能够返回Foobar的字符串值(“ somestring”)?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dtt27783 2017-08-26 03:17
    已采纳

    Convert the value to a string:

    func SomeFunction() string {
            return string(Foobar)
    }
    
    点赞 打赏 评论
  • dongliu1883 2017-08-26 06:12

    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("
    
    ")
            fmt.Println(SomeFunction())
        }
    
        func (c CustomType) String() string {
            fmt.Println("Executing String() for CustomType!")
            return string(c)
        }
    
        func SomeFunction() string {
            return Foobar.String()
        }
    

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

    点赞 打赏 评论
  • du155305 2017-08-26 06:38

    You can convert like this:

    var i int = 42 var f float64 = float64(i)

    check here

    you can return like this:

    return string(Foobar)

    点赞 打赏 评论
  • dongtan6206 2019-02-08 17:31

    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
    

    The Go Playground link

    点赞 打赏 评论

相关推荐