Struct

nil 指针

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Encoder struct{
  6. name string
  7. }
  8. func (e *Encoder) Encode() error {
  9. if e == nil {
  10. e = &Encoder{"nil"}
  11. }
  12. fmt.Println("Encoder name:", e.name)
  13. return nil
  14. }
  15. func main() {
  16. var e *Encoder
  17. e.Encode()
  18. }
  19. // Encoder name: nil

Interface

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Coder interface {
  6. Encode() error
  7. }
  8. type Encoder struct {
  9. name string
  10. }
  11. func (e *Encoder) Encode() error {
  12. if e == nil {
  13. e = &Encoder{"nil"}
  14. }
  15. fmt.Println("Encoder name:", e.name)
  16. return nil
  17. }
  18. func main() {
  19. //⚠️attention: CodeMan's Type is interface
  20. var CodeMan Coder = &Encoder{"man"}
  21. CodeMan.Encode()
  22. // CodeWomen's Type is struct
  23. var CodeWomen *Encoder = &Encoder{"woman"}
  24. CodeWomen.Encode()
  25. }
  26. // Encoder name: man
  27. // Encoder name: woman

Embed

by @fengyfei(fengyfei) @Abser(杨鼎睿)(abser) from GitHub

Anonymous Call

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Anyg struct {}
  6. func (a *Anyg) Any() {
  7. fmt.Println("Any:any")
  8. }
  9. type Someg struct{}
  10. func (a *Someg) Some() {
  11. fmt.Println("Some:some")
  12. }
  13. type All struct{}
  14. func (a *All) Any() {
  15. fmt.Println("All:any")
  16. }
  17. func (a *All) Some() {
  18. fmt.Println("All:some")
  19. }
  20. // a struct include these sturct which have same name methods
  21. type Collection struct {
  22. Someg
  23. p All
  24. Anyg
  25. }
  26. func (c *Collection) Any(){
  27. fmt.Println("collection:any")
  28. }
  29. func (c *Collection) Some() {
  30. fmt.Println("collection:some")
  31. }
  32. // Documents some line to test.
  33. // Then know how can use embed struct.
  34. func main(){
  35. c := &Collection{
  36. Someg{},
  37. All{},
  38. Anyg{},
  39. }
  40. c.Any()
  41. c.Some()
  42. c.p.Any()
  43. c.p.Some()
  44. }
  45. // <---output--->
  46. // collection:any
  47. // collection:some
  48. // All:any
  49. // All:some

Embed to implement Interface

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. // Let's learn this skills how to work in RealWorld
  6. // Type a interface first.
  7. type Ready interface {
  8. Any()
  9. Some()
  10. Readiness() bool
  11. }
  12. // There is a struct implements Ready
  13. type AlwaysReady struct{}
  14. func (a *AlwaysReady)Any() {}
  15. func (a *AlwaysReady)Some() {}
  16. func (a *AlwaysReady) Readiness()bool {
  17. return true
  18. }
  19. // We use All struct to implements our own methods logic.
  20. // and use AlwaysReady to implement Ready interface.
  21. type RealWorldUse struct{
  22. p All
  23. AlwaysReady
  24. }
  25. func EmbedInRealWorld(embedReady Ready){
  26. fmt.Println(embedReady.Readiness())
  27. }
  28. func main() {
  29. embed := &RealWorldUse{
  30. All{},
  31. AlwaysReady{},
  32. }
  33. EmbedInRealWorld(embed)
  34. }
  35. // <---output--->
  36. // true