Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between struct{int} and struct{int int}?

Tags:

types

go

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
}

?

like image 433
Dog Avatar asked Aug 26 '14 21:08

Dog


People also ask

What is the difference between structs and classes?

The only difference between a struct and class in C++ is the default accessibility of member variables and methods. In a struct they are public; in a class they are private.

What is difference between struct and class in C#?

Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types.

What is the difference among a struct a class and an object?

A class has all members private by default. A struct is a class where members are public by default. Classes allow to perform cleanup (garbage collector) before object is deallocated because garbage collector works on heap memory. Objects are usually deallocated when instance is no longer referenced by other code.

Is Int a struct in C#?

5, the simple types provided by C#, such as int , double , and bool , are, in fact, all struct types.


1 Answers

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.

like image 137
peterSO Avatar answered Sep 21 '22 21:09

peterSO