Skip to content

《The Way to Go》中文译本,中文正式名《Go 入门指南》

Notifications You must be signed in to change notification settings

shiyin-weng/the-way-to-go

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

《Go入门指南》

Fork from https:/unknwon/the-way-to-go_ZH_CN for Study Purpose Only

开始阅读

Notes

  • chapter 6: function

    • defer: function or not, implementation: close file, unlock mutual locker...
    • function: closure
  • chapter 7: array and slice

    • concepts of array and slice
      • array: [5]int{1, 2, 3, 4, 5} or [5]int{1, 2} // Fixed length, can not use append(); pass copy as function input
      • slice: arr := make([]int, 10) // dynamic length, pass reference as function input
    • new vs make
      • new(T) assign memory, ptr:nil, len:0, cap:0, return pointer
      • make(T) ptr:[0]int, len:0, cap:0, return value
    • insert item into slice: tricky in golang // empty slice or out of range case
      • reference: /demo/slice.go
  • chapter 8: map // dict in Python, hash or HashTable

    • define:
      • var mp map[string]int
      • mp := make(map[string]int)
  • chapter 9: package

    • sync.Mutex is able to only allow once process accessing data
      	import "sync"
      
      	type Info struct {
      		mu sync.Mutex
      		Str string
      	}
      
      	func Update(info *Info) (
      		info.mu.Lock()
      		info.Str = "update"
      		info.mu.Unlock()
      	)
    • shared buffer
      	type SyncedBuffer struct {
      		lock sync.Mutex
      		buffer bytes.Buffer
      	}
  • chapter 10: struct & method

    • inherit in Go
      	type Engine struct {
      		Start()
      		Stop()
      	}
      
      	type Car struct {
      		Engine
      	}
      
      	func (c *Car) Drive() {
      		c.Start()
      		c.Stop()
      	}
  • chapter 11: interface & reflection

    • polymorphism
      type Shaper interface {
      	Area() float32
      }
      type Square struct {
      	side float32
      }
      func (sq *Square) Area() float32 {
      	return sq.side * sq.side
      }
      type Rectangle struct {
      	length, width float32
      }
      func (r Rectangle) Area() float32 {
      	return r.length * r.width
      }
      
      func main() {
      	r := Rectangle{5, 3}
      	q := &Square{5}
      	shapes := []Shaper{r, q}
      	for n := range shapes {
      		fmt.Println("Shape details: ", shapes[n])
      		fmt.Println("Area of this shape is: ", shapes[n].Area())
      	}
      }
    • type assertion
      if v, ok := varI.(T); ok {
      	// do something
      	return
      }
    • type-switch
      switch t := areaIntf.(type) {
      case *Square:
      	fmt.Printf("Type Square %T with value %v\n", t, t)
      case *Circle:
      	fmt.Printf("Type Circle %T with value %v\n", t, t)
      case nil:
      	fmt.Printf("nil value: nothing to check?\n")
      default:
      	fmt.Printf("Unexpected type %T\n", t)
      }

About

《The Way to Go》中文译本,中文正式名《Go 入门指南》

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 94.4%
  • HTML 2.1%
  • JavaScript 1.5%
  • CSS 1.0%
  • Makefile 0.7%
  • Shell 0.2%
  • Batchfile 0.1%