英文:
assign a string type golang
问题
我在将一个字符串分配给字符串类型时遇到了困难。所以我有这个类型:
type Username string
然后我有一个返回字符串的函数。
我试图做的是将username设置为返回的字符串:
u := &Username{returnedString}
我还尝试过
var u Username
u = returnedString
但我总是得到一个错误。
英文:
I'm having a hard time assigned a string to a type string. So I have this type:
type Username string
I then have a function that returns a string
What I'm trying to do is set username to that returned string:
u := &Username{returnedString}
I also tried
var u Username
u = returnedString
But I always get an error.
答案1
得分: 18
正如其他人指出的那样,你需要进行显式类型转换:
someString := funcThatReturnsString()
u := Username(someString)
我建议阅读这篇文章,了解隐式类型转换。特别是,Honnef在文章中引用了Go的可赋值性规范:
在以下任何情况下,值x都可以赋值给类型为T的变量("x is assignable to T"):
- x的类型V和T具有相同的底层类型,并且V或T中至少有一个不是命名类型。
所以在你的例子中,returnedString
已经是一个"命名类型":它的类型是string
。如果你改为像下面这样做:
var u Username
u = "some string"
那么就没问题了,因为"some string"
会被隐式转换为Username
类型,而string
和Username
都具有底层类型string
。
英文:
As others have pointed out, you'll need to do an explicit type conversion:
someString := funcThatReturnsString()
u := Username(someString)
I'd recommend reading this article on implicit type conversion. Specifically, Honnef references Go's specifications on assignability:
> A value x is assignable to a variable of type T ("x is assignable to T") in any of these cases:
>
> * x's type V and T have identical underlying types and at least one of V or T is not a named type.
So in your example, the returnedString
is already a "named type": it has type string.
If you had instead done something like
var u Username
u = "some string"
you would have been ok, as the "some string"
would be implicitly converted into type Username
and both string
and Username
have the underlying type of string
.
答案2
得分: 12
你创建了一个名为"Username"的用户定义类型。在这种情况下,你不能将一个类型的值(在这里是字符串)直接赋给另一个类型(Username)的变量,除非进行转换。顺便说一下,你可以将字符串字面值赋给它。
var u Username
u = "test"
这样是可以的。
var u Username
var s string
s = "test"
u = s
这样会报错:
无法将类型为string的s分配给类型为Username的变量
你可以转换一个类型到另一个类型,所以...:
var u Username
var s string
s = "test"
u = Username(s)
...也是可以的。"u" 的类型将是Username,值为"test"。
值得注意的是,这些示例并不是惯用的示例。我只是试图清楚地说明这里的类型转换情况。
英文:
You've made a user-defined type "Username". You cannot assign a value of one type to another type (in this case a string to Username) without converting. Incidentally you can assign a string literal to it.
var u Username
u = "test"
Will work.
var u Username
var s string
s = "test"
u = s
Will give you the error:
>cannot use s (type string) as type Username in assignment
You can convert from one type to another, so...:
var u Username
var s string
s = "test"
u = Username(s)
...will also work. "u" will be of type Username with the value "test".
For the record these are not meant as idiomatic examples. I am simply trying to clearly illustrate what's going on with the types here.
答案3
得分: 1
在Go语言中,类型转换的解释可以在《Go之旅》教程中找到。请点击这里。
你可以在这个示例中看到如何使用你的类型。
英文:
Type casting in go is explained in the tutorial "A Tour of Go"
See here for an example using your types.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论