Go Structs: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 5: Line 5:
=Overview=
=Overview=


A <tt>struct</tt> is a user-defined type that contains named fields. It is introduced by the <tt>type</tt> keyword, to indicated that this is a user-defined type, followed by the type name and the keyword <tt>struct</tt>.
A <tt>struct</tt> is a user-defined type that contains named fields. It is introduced by the <tt>type</tt> keyword, to indicated that this is a user-defined type, followed by the type name and the keyword <tt>struct</tt>. Each field has a name and a type.


<pre>
<pre>
Line 11: Line 11:
     i int
     i int
     s string
     s string
}
</pre>
Fields with the same types can be collapsed:
<pre>
type myStruct struct {
    ...
    i, j, k int
  ...
}
}
</pre>
</pre>

Revision as of 02:02, 30 March 2016

Internal

Overview

A struct is a user-defined type that contains named fields. It 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.

type myStruct struct {
    i int
    s string
}

Fields with the same types can be collapsed:

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

Are all users can define (in terms of types) structs, or there are other user-defined types?

Struct Literals

Fields

A field is always exported by the package it is enclosed in.