From 5b5febaed7a6651247fe034b3f59d1cf6d804eeb Mon Sep 17 00:00:00 2001 From: Ryan Kes Date: Mon, 20 May 2024 14:15:07 +0200 Subject: [PATCH] Quartz sync: May 20, 2024, 2:15 PM --- content/20200826151514-variables.md | 30 +++++++- content/20200828181259-structs.md | 115 ++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) diff --git a/content/20200826151514-variables.md b/content/20200826151514-variables.md index 64b3055..443044d 100644 --- a/content/20200826151514-variables.md +++ b/content/20200826151514-variables.md @@ -6,7 +6,8 @@ title: Golang variables # Basics The \`var\` statement declares a list of variables. Type is last. A -\`var\` statement can be at package or function level. +\`var\` statement can be at package or function level. Use var when you +to set a variable type but don't want to set the variable value yet. ``` go package main @@ -21,6 +22,33 @@ func main() { } ``` +This also works with [interfaces](20200831171822-interfaces). + +``` go +package main + +import "fmt" + +type printSomething interface { + print() +} + +type agent struct { +} + +func (d *agent) print() { + fmt.Println("tralala") +} + +var ( + a = &agent{} +) + +func main() { + a.print() +} +``` + # Initializers A var declaration can include initializers, one per variable. If an diff --git a/content/20200828181259-structs.md b/content/20200828181259-structs.md index a82f8a1..3a86534 100644 --- a/content/20200828181259-structs.md +++ b/content/20200828181259-structs.md @@ -68,3 +68,118 @@ func main() { fmt.Println(v1, p, v2, v3) } ``` + +# 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)) +} +```