go - assign a string type golang -
i'm having hard time assigned string type string. have type:
type username string
i have function returns string
what i'm trying set username returned string:
u := &username{returnedstring}
i tried
var u username u = returnedstring
but error.
as others have pointed out, you'll need 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 assignable variable of type t ("x assignable t") in of these cases:
- x's type v , t have identical underlying types , @ least 1 of v or t not named type.
so in example, returnedstring
"named type": has type string.
if had instead done like
var u username u = "some string"
you have been ok, "some string"
implicitly converted type username
, both string
, username
have underlying type of string
.
Comments
Post a Comment