capypad
0 day streak
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
go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main
 
import "fmt"
 
type User struct {
Username string
Email string
Active bool
}
 
func main() {
u := User{Username: "coder123", Email: "[email protected]", Active: true}
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.