https://www.gravatar.com/avatar/f819a092b23cb25a09f87436f68df217?s=240&d=mp

Ang Gao

Software Engineer @ TripAdvisor, CS Ph.D @ University College Cork

Golang Note Five

Select The select statement lets a goroutine wait on multiple communication operations. A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready. Default Selection The default case in a select is run if no other case is ready. Use a default case to try a send or receive without blocking: 1 2 3 4 5 6 select { case i := <-c: // use i default: // receiving from c would block }

Golang Note Four

Goroutines A goroutine is a lightweight thread managed by the Go runtime. 1 go f(x, y, z) The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine. Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won’t need them much in Go as there are other primitives.

Golang Note Three

Methods Go does not have classes. However, you can define methods on struct types. The method receiver appears in its own argument list between the func keyword and the method name. You can declare a method on any type that is declared in your package, not just struct types. You cannot define a method on a type from another package (including built in types). Methods can be associated with a named type or a pointer to a named type.

Golang Note Two

Flow control statements Go has only one looping construct, the for loop. For is Go’s “while” Like for, the if statement can start with a short statement to execute before the condition. A case body breaks automatically, unless it ends with a fallthrough statement. A defer statement defers the execution of a function until the surrounding function returns. The deferred call’s arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

Golang Note One

Basics Every Go program is made up of packages. Programs start running in package main. factored import statement. 1 2 3 4 import ( "fmt" "math" ) In Go, a name is exported if it begins with a capital letter. Functions in Golang 1 2 3 func add(x int, y int) int { return x + y } When two or more consecutive named function parameters share a type, you can omit the type from all but the last.

Jackson: JSON parsing and generation library

使用 Jackson 加入jar 包: jackson-annotation-2.2.3.jar jackson-core-2.2.3.jar jackson-databind-2.2.3.jar 创建 com.fasterxml.jackson.databind.ObjectMapper 调用ObjectMapper 的 writeValueAsString 方法把Java对象或集合转为json字符串. Jackson 根据getter来定位Java