mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:21:38 +08:00
update book code
This commit is contained in:
38
eBook/examples/chapter_10/embed_func2.go
Normal file
38
eBook/examples/chapter_10/embed_func2.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Log struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
type Customer struct {
|
||||
Name string
|
||||
Log
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := &Customer{"Barak Obama", Log{"1 - Yes we can!"}}
|
||||
c.Add("2 - After me the world will be a better place!")
|
||||
fmt.Println(c)
|
||||
}
|
||||
|
||||
func (l *Log) Add(s string) {
|
||||
l.msg += "\n" + s
|
||||
}
|
||||
|
||||
func (c *Customer) String() string {
|
||||
return c.Name + "\nLog:" + fmt.Sprintln(c.Log)
|
||||
}
|
||||
|
||||
func (l *Log) String() string {
|
||||
return l.msg
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Barak Obama
|
||||
Log:{1 - Yes we can!
|
||||
2 - After me the world will be a better place!}
|
||||
*/
|
Reference in New Issue
Block a user