A functional literal just represents an anonymous function. You can assign functional literal to a variable:
package main import ( "fmt" ) func main() { f := func() { fmt.Println("Hello, भारत!") } f() } Or invoke functional literal directly (Please notice the () at the end of functional literal):
package main import ( "fmt" ) func main() { func() { fmt.Println("Hello, भारत!") }() } The above 2 programs both output “Hello, भारत!”.
[Read More]
Go - init function
There is a init() function, as the name suggests, it will do some initialization work, such as initializing variables which may not be expressed easily, or calibrating program state. A file can contain one or more init() functions, as shown here:
package main import "fmt" var global int = 0 func init() { global++ fmt.Println("In first Init(), global is: ", global) } func init() { global++ fmt.Println("In Second Init(), global is: ", global) } func main() { fmt.
[Read More]
Go - Basic Data Type
Following are the basic data types in Go:
Numeric // Integer Types uint8 // Unsigned 8-bit integers (0 to 255) uint16 // Unsigned 16-bit integers (0 to 65535) uint32 // Unsigned 32-bit integers (0 to 4294967295) uint64 // Unsigned 64-bit integers (0 to 18446744073709551615) int8 // Signed 8-bit integers (-128 to 127) int16 // Signed 16-bit integers (-32768 to 32767) int32 // Signed 32-bit integers (-2147483648 to 2147483647) int64 // Signed 64-bit integers (-9223372036854775808 to 9223372036854775807) // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Floating Types float32 // IEEE-754 32-bit floating-point numbers float64 // IEEE-754 64-bit floating-point numbers complex64 // Complex numbers with float32 real and imaginary parts complex128 // Complex numbers with float64 real and imaginary parts // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Other Numeric Types byte // same as uint8 rune // same as int32 uint // 32 or 64 bits int // same size as uint uintptr // an unsigned integer to store the uninterpreted bits of a pointer value Handling Overflows At compile time, a Go compiler can catch overflow errors.
[Read More]
Go - Data Type Intro
There are following data types in Go:
- Basic type: Numbers, strings, and booleans come under this category.
- Aggregate type: Array and structs come under this category.
- Reference type: Pointers, slices, maps, functions, and channels come under this * category.
- Interface type
Go - defer
The defer statement is used to postpone a function call executed immediately before the surrounding function returns. The common uses of defer include releasing resources (i.e., unlock the mutex, close file handle.), do some tracing(i.e., record the running time of function), etc. E.g., an ordinary accessing global variable exclusively is like this:
var mu sync.Mutex var m = make(map[string]int) func lookup(key string) int { mu.Lock() v := m[key] mu.Unlock() return v } An equivalent but concise format using defer is as follow:
[Read More]
Go - literals
literal means the value itself. Unlike variable, a literal doesn’t have a name.
Go - Mutability
In Go, only constants are immutable. However because arguments are passed by value, a function receiving a value argument and mutating it, won’t mutate the original value.
package main import "fmt" type Artist struct { Name, Genre string Songs int } func newRelease(a Artist) int { a.Songs++ return a.Songs } func main() { me := Artist{Name: "Matt", Genre: "Electro", Songs: 42} fmt.Printf("%s released their %dth song\n", me.Name, newRelease(me)) fmt.Printf("%s has a total of %d songs", me.
[Read More]
Go - Named and Unnamed Types
Types in Go are divided into 2 categories: named and unnamed. Besides predeclared types (such as int, rune, etc), you can also define named type yourself. E.g.:
type mySlice []int Unnamed types are defined by type literal. I.e., []int is an unnamed type.
According to Go spec, there is an underlying type of every type:
Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself.
[Read More]
Go - Processing JSON object
JSON is a commonly used and powerful data-interchange format, and Go provides a built-in json package to handle it. Let’ see the following example:
package main import ( "encoding/json" "log" "fmt" ) type People struct { Name string age int Career string `json:"career"` Married bool `json:",omitempty"` } func main() { p := &People{ Name: "Nan", age: 34, Career: "Engineer", } data, err := json.Marshal(p) if err != nil { log.Fatalf("JSON marshaling failed: %s", err) } fmt.
[Read More]
Go - Slice copy
The definition of built-in copy function is here:
func copy(dst, src []Type) int
The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes from a string to a slice of bytes.) The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).
Let’s see a basic example in which source and destination slices aren’t overlapped:
[Read More]