Giter Club home page Giter Club logo

gobyexample's People

Contributors

hhow09 avatar

Watchers

 avatar

gobyexample's Issues

Reflection

Reflection

Overview

  • interface values is composed of (value, type)
  • Reflection is a type of meta programming
  • any interface values can be boxed into reflect.Value and reflect.Type

Laws of Reflection

1. Reflection goes from interface value to reflection object.

2. Reflection goes from reflection object to interface value.

3. To modify a reflection object, the value must be settable.

func (v Value) CanSet() bool
    CanSet reports whether the value of v can be changed. A Value can be
    changed only if it is addressable and was not obtained by the use of
    unexported struct fields. If CanSet returns false, calling Value.Set or any
    type-specific setter (e.g., Value.SetBool, Value.SetInt) will panic.
  • addressable: need to be pointer variable

Kind

Although there are infinitely many types, there are only a finite number of kinds of type: the basic types Bool, String and all the numbers; the aggregate types Array and Struct, the reference types Chan, Func, Ptr, Slice and Map; interface types; and finally Invalid, meaning no value at all (The zero value of a reflect.Value has kind Invalid).

Reference

interview

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

Channel

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.