string 在 Go 中将自定义类型转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45891600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:38:23  来源:igfitidea点击:

Converting a custom type to string in Go

stringgotype-conversion

提问by DaveUK

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

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

How would you fix SomeFunction so that it is able to return the string value of Foobar ("somestring") ?

您将如何修复 SomeFunction 以便它能够返回 Foobar ("somestring") 的字符串值?

回答by Cerise Limón

Convertthe value to a string:

转换为字符串:

func SomeFunction() string {
        return string(Foobar)
}

回答by Ravi

Better to define a Stringfunction 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 SomeFunctionthen let it return Foobar.String()

更好地定义一个String函数Customtype- 随着时间的推移,它可以让你的生活更轻松 - 当结构发生变化时,你可以更好地控制事物。如果你真的需要,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()
    }

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

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

回答by Ashish Tiwari

You can convert like this:

你可以这样转换:

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

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

check here

在这里检查

you can return like this:

你可以这样返回:

return string(Foobar)

return string(Foobar)

回答by Krishnadas PC

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

对于每个类型 T,都有一个相应的转换操作 T(x) 将值 x 转换为类型 T。如果两者具有相同的基础类型,或者如果两者都是指向的未命名指针类型,则允许从一种类型转换为另一种类型到相同基础类型的变量;这些转换改变了类型而不是值的表示。如果 x 可分配给 T,则允许进行转换,但通常是多余的。- 摘自Go 编程语言 - 作者:Alan AA 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

Go Playground 链接