[Go Spec]Confused about Assignability in Golang Specification

109 views
Skip to first unread message

Jimu Yang

unread,
Feb 23, 2020, 8:19:34 AM2/23/20
to golang-nuts
I am reading the doc about Assignability in Golang Specification . It says:

A value x is assignable to a variable of type T ("x is assignable to T") if one of the following conditions applies:

  • ...
  • x's type V and T have identical underlying types and at least one of V or T is not a defined type.
  • ...

type str string

func main() {
string1 := "string"
var str1 str = "str1"
str1 = string1 // cannot use string1 (type string) as type str in assignment
}


string1's type string and str have identical underlying type (string) and string is not a defined type. 

So why cannot i assign string1 to str1?

Jan Mercl

unread,
Feb 23, 2020, 8:53:44 AM2/23/20
to Jimu Yang, golang-nuts
Both string1 and str1 have defined types while the quoted specs rule says "at least one of V or T is not a defined type.".

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8fedba03-5aa1-4227-80c5-686a87a45cdd%40googlegroups.com.

Ian Lance Taylor

unread,
Feb 23, 2020, 8:59:36 AM2/23/20
to Jimu Yang, golang-nuts
On Sun, Feb 23, 2020 at 5:19 AM Jimu Yang <jimu...@gmail.com> wrote:
>
> string1's type string and str have identical underlying type (string) and string is not a defined type.

"string" is a defined type. See https://golang.org/ref/spec#String_types.

Ian

Michael Poole

unread,
Feb 23, 2020, 9:00:15 AM2/23/20
to Jimu Yang, golang-nuts
According to the language specification (https://golang.org/ref/spec#String_types), string is a defined type, and so it is distinct from your type str.

Jimu Yang

unread,
Feb 23, 2020, 11:20:42 AM2/23/20
to golang-nuts
Thank you all. 
i read all about defined types on the doc https://golang.org/ref/spec#String_types 
now i know many defined types (string bool int ...      but which are the "undefined" types? i didn't find a direct answer. 
my thinking is that the "literals" which including all type literals are not defined types. The following code confirms that.
could you give me a reference answer? 

type str string
var s str = "sdwf"

// boolean literal
type boolean bool
var b boolean = true

// integer literal
type integer int
var i integer = 1

// slice literal
type strarr []string
var strings []string = []string{"sfw"}
var strs strarr = strings

// pointer literal
type stringptr *string
var string1 string = "11"
var ptr stringptr = &string1






在 2020年2月23日星期日 UTC+8下午9:19:34,Jimu Yang写道:

Jan Mercl

unread,
Feb 23, 2020, 11:32:06 AM2/23/20
to Jimu Yang, golang-nuts
Type literals are a prime example of "undefined" types. For example as in `var a []int`.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages