mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-25 04:56:23 +00:00
Quartz sync: May 20, 2024, 2:15 PM
This commit is contained in:
parent
561cc620f4
commit
5b5febaed7
2 changed files with 144 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue