Go Structs: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 35: Line 35:
</pre>
</pre>


==Filed Tag and Attributes==
==Filed Tags==


See
See


<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[#Filed_Tag_and_Attributes|Filed Tag and Attributes]]
:[[#Field_Tags_and_Attributes|Filed Tag and Attributes]]
</blockquote>
</blockquote>



Revision as of 01:02, 3 April 2016

Internal

Overview

A struct is a user-defined type that contains named typed fields (and possibly other type names, see embedded types below). structs may also declare methods that operate on that data. The fields usually represent a has-a relationship. structs can also have methods associated with them.

Questions

  • Are all users can define (in terms of types) structs, or there are other user-defined types?
  • What is the difference between the methods that have the type as receiver and the function declared as fields by the type?

Declaration

The structs can be declare at package level or inside a function. The struct type definition is introduced by the type keyword, to indicated that this is a user-defined type, followed by the type name and the keyword struct. Each field has a name and a type. The type can be a built-in type or a user-defined type, such as another struct or an interface.

type myStruct struct {
    i int
    s string
}

Fields with the same types can be collapsed:

type myStruct struct {
    ...
    i, j, k int
   ...
}

Filed Tags

See

Filed Tag and Attributes

Initialization

Long Variable Declaration

var ms myStruct

If no explicit initialization follows, all the struct's fields are initialized with their zero value.

Short Variable Declaration

Literal

Struct literal initialization:

ms := myStruct{i: 5, s: "something"}

There is a shorter struct literal where the name of the fields are omitted, provided that the order is maintained:

ms := myStruct{5, "something"}

new()

new() initialization:

msPtr := new(myStruct)

Note new() returns a pointer to the structure and not the structure itself.

Fields

Fields can be access with the . operator.

Note that the . operator works with a regular struct variable as well as with a pointer to a struct: the compiler knows how to compensate for both of these cases.

ms := myStruct{1, "s"}
msPtr := new(myStruct)

// valid:
ms.i

// valid:
msPtr.i

Even if the enclosing struct type is exported by a package, not all fields are exported automatically, only those whose first character is an upper case letter. This behavior makes possible to have "private" fields in a public structure, when the structure is used outside its package.

Field Tags and Attributes

A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored.

Example

type A struct {
    name string `json:"name"`
}

Embedded Types

An embedded type is initialized by declaring a name of a struct inside other struct - without associating it with a field. An embedded type is sometimes referred to as anonymous field. It models an is-a relationship. Example:

type A struct {
    f1 int
}

type B struct {
    A
    f2 int
}

Methods and Embedded Types

A pointer receiver method associated with the embedded type also works with the embedding type. For example, if there's a:

func (a *A) m1(...) {
    ...
}

m1() also works directly with B instances as such:

bPtr := new(B)
...
bPtr.m1(...)

In the code sequence above, the invocations bPtr.m1(...) and bPtr.A.m1(...) are equivalent.