2024-05-06 20:40:05 +00:00
|
|
|
---
|
2024-10-30 17:04:36 +00:00
|
|
|
date: 20200828
|
2024-05-06 20:40:05 +00:00
|
|
|
id: 463ce364-f426-4089-a0ca-ffe45f0461f2
|
|
|
|
title: Golang Structs
|
|
|
|
---
|
|
|
|
|
|
|
|
# Basics
|
|
|
|
|
|
|
|
Golang structs are what you expect
|
|
|
|
|
|
|
|
``` go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
type Vertex struct {
|
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println(Vertex{1, 2})
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# Struct fields
|
|
|
|
|
|
|
|
Struct fields are accessed using a dot
|
|
|
|
|
|
|
|
``` go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
type Coorindate struct {
|
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
coordinate := Coorindate{1, 67}
|
|
|
|
coordinate.X = 26
|
|
|
|
fmt.Println(coordinate.X)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# Struct literals
|
|
|
|
|
|
|
|
You can also newly allocate a struct by listing the values of its
|
|
|
|
fields. This is known as a "struct literal" (duh)
|
|
|
|
|
|
|
|
``` go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
type Vertex struct {
|
|
|
|
X, Y int
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
v1 = Vertex{1, 2} // has type Vertex
|
|
|
|
v2 = Vertex{X: 1} // Y:0 is implicit
|
|
|
|
v3 = Vertex{} // X:0 and Y:0
|
|
|
|
p = &Vertex{1, 2} // has type *Vertex
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println(v1, p, v2, v3)
|
|
|
|
}
|
|
|
|
```
|
2024-05-20 12:15:07 +00:00
|
|
|
|
|
|
|
# Struct tags
|
|
|
|
|
|
|
|
[Struct tags](https://go.dev/wiki/Well-known-struct-tags) allow other
|
|
|
|
modules accessing struct members to format member values.
|
|
|
|
|
|
|
|
## Control encoding
|
|
|
|
|
|
|
|
``` go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
PreferredFish []string `json:"preferredFish"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
u := &User{
|
|
|
|
Name: "Sammy the Shark",
|
|
|
|
Password: "fisharegreat",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.MarshalIndent(u, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Remove empty fields
|
|
|
|
|
|
|
|
``` go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
PreferredFish []string `json:"preferredFish,omitempty"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
u := &User{
|
|
|
|
Name: "Sammy the Shark",
|
|
|
|
Password: "fisharegreat",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.MarshalIndent(u, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Ignore private fields
|
|
|
|
|
|
|
|
``` go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Password string `json:"-"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
u := &User{
|
|
|
|
Name: "Sammy the Shark",
|
|
|
|
Password: "fisharegreat",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.MarshalIndent(u, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
}
|
|
|
|
```
|