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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -