英文:
How can I convert a custom type to a string?
问题
我正在使用一个包,但它返回的不是一个字符串,而是一个economy.ClassId
。你可以在这里看到。
我想将economy.ClassId
传递给一个接受字符串而不是economy.ClassId
的函数,所以有没有办法将这种类型转换为字符串。谢谢。
我知道你可以使用strconv.FormatUint()
,但对于自定义类型应该使用什么呢?
英文:
I'm using a package and instead of returning a string
, it returns a economy.ClassId
You can see here
I want to pass the economy.ClassId
which is just a string of numbers like "531569319" to a function that takes a string not a economy.ClassId
so is there a way to convert this type to a string or no. Thanks.
I know you could like strconv.FormatUint()
but what would I use for a custom type.
答案1
得分: 6
更一般地,你可以在你的类型上实现Stringer接口。
func (c ClassId) String() string {
return strconv.FormatUint(uint64(c), 10)
}
然后在其他函数中使用classId.String()
。
PS:缩写(ID)也应该全部大写。
参考链接:http://talks.golang.org/2014/names.slide#6
英文:
more generally you could implement the stringer interface on your type https://golang.org/pkg/fmt/#Stringer
func (c ClassId) String() string {
return strconv.FormatUint(uint64(c),10)
}
and use that otherFunc(classId.String())
http://play.golang.org/p/cgdQZNR8GF
PS: also acronymes (ID) should be all capitals
答案2
得分: 5
ClassId
声明为 type ClassId uint64
,所以尽管 ClassId
不完全等同于 uint64
,它们具有相同的底层类型,因此可以从一个类型转换为另一个类型。要将值 x
转换为类型 T
(如果允许),可以写作 T(x)
。所以,在你的情况下:
funcThatTakesAString(strconv.FormatUint(uint64(classId), 10))
英文:
ClassId
is declared as type ClassId uint64
, so although a ClassId
isn't exactly the same as a uint64
, they have the same underlying type, and you can therefore convert from one to the other. To convert a value x
to type T
(when permitted), you write T(x)
. So, in your case:
funcThatTakesAString(strconv.FormatUint(uint64(classId), 10))
答案3
得分: 0
另一种更符合惯用法的方法是使用Go的fmt.Sprintf函数和格式化字符串规范。
两个示例...
以下代码将classId转换为一个十进制字符串:
classIDStr := fmt.Sprintf("%d", classId)
以下代码将classId转换为一个十六进制字符串,其中小写字母为a-f:
classIDHex := fmt.Sprintf("%x", classId)
英文:
Another approach that is perhaps more idiomatic is to use fmt.Sprintf using Go's format string specification.
Two examples...
The following converts the classId into a string base 10 number:
classIDStr := fmt.Sprintf("%d", classId)
The following converts it into a string hexadecimal number, with a-f digits in lower case:
classIDHex := fmt.Sprintf("%x", classId)
答案4
得分: 0
这是一个将具有基础string
类型的自定义类型转换回string
的示例:
type CustomType string
const (
StereoType CustomType = "stereotype"
ArcheType CustomType = "archetype"
)
func printString(word string) {
println(word)
}
现在,如果你调用printString(StereoType)
,你将得到一个编译错误,错误信息如下:
cannot use StereoType (constant "stereotype" of type CustomType) as type string in argument to printString
解决方案是将其强制转换回string
,像这样:
printString(string(StereoType))
英文:
Here's an example of converting custom types which have an underlying string
type back to string
:
type CustomType string
const (
StereoType CustomType = "stereotype"
ArcheType CustomType = "archetype"
)
func printString(word string) {
println(word)
}
Now if you call printString(StereoType)
, you'll get a compilation error saying
cannot use StereoType (constant "stereotype" of type CustomType) as type string in argument to printString
The solution is to cast it back to a string
like this:
printString(string(StereoType))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论