mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-15 19:12:35 +00:00
good good study, day day up!
This commit is contained in:
46
vendor/gopl.io/ch4/embed/main.go
generated
vendored
Normal file
46
vendor/gopl.io/ch4/embed/main.go
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 106.
|
||||
|
||||
// Embed demonstrates basic struct embedding.
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Point struct{ X, Y int }
|
||||
|
||||
type Circle struct {
|
||||
Point
|
||||
Radius int
|
||||
}
|
||||
|
||||
type Wheel struct {
|
||||
Circle
|
||||
Spokes int
|
||||
}
|
||||
|
||||
func main() {
|
||||
var w Wheel
|
||||
//!+
|
||||
w = Wheel{Circle{Point{8, 8}, 5}, 20}
|
||||
|
||||
w = Wheel{
|
||||
Circle: Circle{
|
||||
Point: Point{X: 8, Y: 8},
|
||||
Radius: 5,
|
||||
},
|
||||
Spokes: 20, // NOTE: trailing comma necessary here (and at Radius)
|
||||
}
|
||||
|
||||
fmt.Printf("%#v\n", w)
|
||||
// Output:
|
||||
// Wheel{Circle:Circle{Point:Point{X:8, Y:8}, Radius:5}, Spokes:20}
|
||||
|
||||
w.X = 42
|
||||
|
||||
fmt.Printf("%#v\n", w)
|
||||
// Output:
|
||||
// Wheel{Circle:Circle{Point:Point{X:42, Y:8}, Radius:5}, Spokes:20}
|
||||
//!-
|
||||
}
|
Reference in New Issue
Block a user