go / beginner
Snippet
Defining Structs
Structs are used to group related data together. Unlike classes in other languages, Go uses structs to define the shape of data objects.
snippet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package mainimport "fmt"type User struct {Username stringEmail stringActive bool}func main() {fmt.Println(u.Username)}
Breakdown
1
type User struct {
Defines a new custom data type named User using a structure.
2
Username string
A field in the struct that stores text data.
3
u := User{...}
Creates an instance of the User struct with specific values.