March 2019
Beginner to intermediate
324 pages
7h 17m
English
A struct in Go is a data structure that is composed of fields, where each field has a type. Here is what a Go struct looks like:
type myStruct struct{ intField int stringField string sliceField []int}
The preceding code creates a struct type that is called myStruct, which contains three fields:
You can then initialize and use that struct type in your code:
var s = myStruct{intField: 3,stringField: "three",sliceField : []int{1,2,3},}
The preceding method of initialization is also known as struct literals. There is a shorter version of it that looks like this:
var s = myStruct{3,"three",[]int{1,2,3}}
You can also use what is known as dot notation, which looks ...
Read now
Unlock full access