Structures
Structures in Go are both very powerful and very popular and are used for organizing and grouping various types of data under the same name. Structures are the more versatile data type in Go—they can even have associated functions, which are called methods.
Structures, as well as other user-defined data types, are usually defined outside the main()
function or any other package function so that they can have a global scope and be available to the entire Go package. Therefore, unless you want to make clear that a type is only useful within the current local scope and is not expected to be used elsewhere, you should write the definitions of new data types outside functions.
The type keyword
The type
keyword allows you to define new data types or create aliases for existing ones. Therefore, you are allowed to say type myInt int
and define a new data type called myInt
, which is an alias for int
. However, Go considers myInt
and int
as totally different data...