系列: Go
go
69 行
· 更新于 2026-04-18
main.go
Go/part1-language/examples/04_types/main.go
// Demonstrates built-in types: numbers, strings, arrays, slices, maps, structs.
package main
import (
"fmt"
"strings"
)
type Point struct {
X, Y int
}
type Animal struct{ Name string }
func (a Animal) Speak() string { return a.Name + " makes a sound" }
type Dog struct {
Animal
Breed string
}
func main() {
var i int = 65
f := float64(i)
r := rune(i)
fmt.Printf("int=%d float=%v rune=%c\n", i, f, r)
s := "héllo"
fmt.Println("byte len:", len(s))
for i, r := range s {
fmt.Printf(" byte %d -> %c (U+%04X)\n", i, r, r)
}
var b strings.Builder
for k := 0; k < 5; k++ {
fmt.Fprintf(&b, "x%d ", k)
}
fmt.Println("built:", b.String())
arr := [3]int{1, 2, 3}
cp := arr
cp[0] = 99
fmt.Println("array value semantics:", arr, cp)
sl := []int{1, 2, 3, 4, 5}
sub := sl[1:4]
sub[0] = 99
fmt.Println("slices share backing:", sl, sub)
m := map[string]int{"a": 1, "b": 2}
m["c"] = 3
if v, ok := m["x"]; ok {
fmt.Println("found", v)
} else {
fmt.Println("'x' missing, zero is", v)
}
delete(m, "a")
fmt.Println("map:", m)
p := Point{X: 1, Y: 2}
fmt.Println("struct:", p)
d := Dog{Animal: Animal{Name: "Rex"}, Breed: "Lab"}
fmt.Println("embedded:", d.Name, "|", d.Speak(), "|", d.Breed)
q := &Point{3, 4}
q.X = 30
fmt.Println("pointer:", *q)
}
相关文章
Go
go
更新于 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/hashpw/main.go).
阅读文章 →
Go
go
更新于 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/server/main.go).
阅读文章 →
Go
go
更新于 2026-04-18
docs.go
docs.go — go source code from the Go learning materials (Go/Baasid/docs/docs.go).
阅读文章 →
Go
go
更新于 2026-04-18
jwt.go
jwt.go — go source code from the Go learning materials (Go/Baasid/internal/auth/jwt.go).
阅读文章 →
Go
go
更新于 2026-04-18
password.go
password.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password.go).
阅读文章 →
Go
go
更新于 2026-04-18
password_test.go
password_test.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password_test.go).
阅读文章 →