dqc3469 2014-08-26 21:44
浏览 115

struct {int}和struct {int int}有什么区别?

What is the difference between these two structs other than that they aren't considered equivalent?

package main
import "fmt"
func main() {
    a := struct{int}{1}
    b := struct{int int}{1}
    fmt.Println(a,b)
    a.int=2
    b.int=a.int
    fmt.Println(a,b)
    //a = b
}

They look the same:

$ go run a.go 
{1} {1}
{2} {2}

But if you uncomment a = b, it says:

$ go run a.go 
# command-line-arguments
./a.go:10: cannot use b (type struct { int int }) as type struct { int } in assignment

Yet struct{a,b int} and struct{a int;b int} are equivalent:

package main

func main() {
    a := struct{a,b int}{1,2}
    b := struct{a int;b int}{1,2}
    a = b
    b = a
}

?

  • 写回答

1条回答 默认 最新

  • dongwei6457 2014-08-26 21:51
    关注

    struct { int } is a struct with an anonymous field of type int.

    struct { int int } is a struct with a field named int of type int.

    int is not a keyword; it can be used as an identifier.

    The struct types are not identical; the corresponding fields do not have the same names.

    A field declared with a type but no explicit field name is an anonymous field and the unqualified type name acts as the anonymous field name. Therefore, field names a.int and b.int are valid. For example,

    a := struct{ int }{1}
    b := struct{ int int }{1}
    a.int = 2
    b.int = a.int
    

    The Go Programming Language Specification

    Struct types

    A struct is a sequence of named elements, called fields, each of which has a name and a type. Field names may be specified explicitly (IdentifierList) or implicitly (AnonymousField). Within a struct, non-blank field names must be unique.

    StructType     = "struct" "{" { FieldDecl ";" } "}" .
    FieldDecl      = (IdentifierList Type | AnonymousField) [ Tag ] .
    AnonymousField = [ "*" ] TypeName .
    

    A field declared with a type but no explicit field name is an anonymous field, also called an embedded field or an embedding of the type in the struct. An embedded type must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.

    Keywords

    The following keywords are reserved and may not be used as identifiers.

    break        default      func         interface    select
    case         defer        go           map          struct
    chan         else         goto         package      switch
    const        fallthrough  if           range        type
    continue     for          import       return       var
    

    Type identity

    Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags. Two anonymous fields are considered to have the same name. Lower-case field names from different packages are always different.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况