Skip to content

Latest commit

 

History

History
118 lines (102 loc) · 4.02 KB

File metadata and controls

118 lines (102 loc) · 4.02 KB

Interview Questions

Data structure

  1. 如果range over slice時對slice作改動,是否會改動slice ? range over slice will copy the original value.

  2. how to compare 2 struct value ? reflect.DeepEqual

  3. how to verify that struct actually implement a Interface ? var _ Interface = (*Struct_Should_Implement)(nil)

  4. convert []byte to string 是否會造成 copy ?

    • yes
    • string is immutable but []byte is mutable
  5. Modifying map while iterating over it ?

    • unpredictable
    • If a map entry is created during iteration, that entry may be produced during the iteration or may be skipped.
    • If a map entry that has not yet been reached is removed during iteration, the corresponding iteration value will not be produced.
  6. How to check if a slice is sorted ?

  7. marshal json what is the result ?

type J struct {
	a string 
	b string `json:"B"`
	C string
	D string `json:"DD"`
}
	j := J{
		a: "1",
                 b: "2",
		C: "3",
		D: "4",
	}
	fmt.Printf("j = %+v\n", j) 
  1. make vs new

    • both for memory allocation
    • new will allocate the zero value of type
    • make can only allocate map, slice, chan
  2. can we address the map ?

    • map is not addressable
    • when hash table is reorganized, the address will change.
    m := make(map[string]int)
    fmt.Println(&m["qcrao"])
    // cannot take the address of m["qcrao"]
  3. use case of tag

    • json marshall unmarshal
    • db: sqlx column mapping
    • form: gin validation for form
    • binding: gin binding for validation
  4. fmt.Printf with %v %+v %#v

type animal struct {
	name string
	age  int
}

a := animal{name: "cat", age: 2}
fmt.Printf("%v\n", a) // {cat 2}
fmt.Printf("%+v\n", a) // {name:cat age:2}
fmt.Printf("%#v\n", a) // main.animal{name:"cat", age:2}

Functions

  1. the execution order of import, const, var, init(), main() import –> const –> var –>init()–>main()

Concurrency

  1. Channel deadlock的情況
    • read from nil channel
    • write to full channel
    • write to closed channel
    • all grouting blocked
  2. 讀closed channel 的行為?
    • 一直讀到 zero value
    • 不會deadlock
    • 解決: 把原chan 設為 nil

Concurrency control

  1. use wait group to wait for multiple goroutines returns
  2. use channel to block channel sender or channel receiver
  3. use channel if we need specific order of goroutine execution
  4. pass context within chain of goroutine to control / cancel .
  5. use select to organize multiple channels

Mutex

Design Pattern

Internals

Map

  1. map is hmap struct with pointer buckets to buckets array of 2^B buckets.
  2. bucket is bmap struct with 8 buckets, each bucket stores an key value pair

Web

  1. Graceful shutdown
    1. start a goroutine for server.listenAndServe() in background
    2. main thread blocked and listen to os termination signal (syscall.SIGTERM )
    3. if received os signal, use context with timeout and call server to shutdown.

Ref