Build Go development environment is always easy. Take Linux OS as an example (Because I work as a root user, so if you login as a non-root user, maybe you need sudo to execute some commands), what you should do is just download the binary package which matches your system from here, and uncompress it:
# wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz # tar -C /usr/local/ -xzf go1.6.2.linux-amd64.tar.gz Now, there is an extra go directory under /usr/local.
[Read More]
Install Golang
Installing using OS package manager You might use your package manager to install Go or install manually from the official release binaries. Either way should be easy.
Because the go tool is able to download code from remote repositories, it is often useful to have installed clients for the various supported version control systems. At the time of this writing, they are documented as: Bazaar, Git, Mercurial and Subversion.
They are not mandatory requirements, you might go a long way with just having git installed, and installing others later as required.
[Read More]
io.Reader interface
io.Reader interface is a basic and very frequently-used interface:
type Reader interface { Read(p []byte) (n int, err error) } For every type who satisfies the io.Reader interface, you can imagine it’s a pipe. Someone writes contents into one end of the pipe, and you can use Read() method which the type has provided to read content from the other end of the pipe. No matter it is a common file, a network socket, and so on.
[Read More]
io.Writer interface
The inverse of io.Reader is io.Writer interface:
type Writer interface { Write(p []byte) (n int, err error) } Compared to io.Reader, since you no need to consider io.EOF error, the process of Write method is simple:
(1) err == nil: All the data in p is written successfully;
(2) err != nil: The data in p is partially or not written at all.
Let’s see an example:
package main import ( "log" "os" ) func main() { f, err := os.
[Read More]
Method with struct and interface
Methods enhance types with additional behavior.
Methods of the type are called Method Set.
To attach method to a type :
// Syntax // “varName Type” is called a “receiver” func (varName Type) funcName() { // Code }
// Example // “book” is a struct here func (b book) printBook() { fmt.Println(b.title, b.price) }
- A `receiver` is nothing but method's `input parameters` written `before` a `method name`. - A `method` belongs to a `single type`.
[Read More]
Nil values
Pass slice as a function argument
In Go, the function parameters are passed by value. With respect to use slice as a function argument, that means the function will get the copies of the slice: a pointer which points to the starting address of the underlying array, accompanied by the length and capacity of the slice. Oh boy! Since you know the address of the memory which is used to store the data, you can tweak the slice now.
[Read More]
Prepend in Slice
Go has a built-in append function which add elements in the slice:
func append(slice []Type, elems …Type) []Type
But how if we want to the “prepend” effect? Maybe we should use copy function. E.g.:
package main import "fmt" func main() { var s []int = []int{1, 2} fmt.Println(s) s1 := make([]int, len(s) + 1) s1[0] = 0 copy(s1[1:], s) s = s1 fmt.Println(s) } The result is like this:
[1 2] [0 1 2] But the above code looks ugly and cumbersome, so an elegant implementation maybe here:
[Read More]
Printf and Sprintf formatting
Following formatting can be used with fmt.Printf as well as fmt.Sprintf:
// String and slice of bytes %s // the uninterpreted bytes of the string or slice %q // a double-quoted string safely escaped with Go syntax %x // base 16, lower-case, two characters per byte %X // base 16, upper-case, two characters per byte // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Boolean %t // the word true or false // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // General %v // The value in a default format.
[Read More]
Reallocating underlying array of slice
When appending data into slice, if the underlying array of the slice doesn’t have enough space, a new array will be allocated. Then the elements in old array will be copied into this new memory, accompanied with adding new data behind. So when using Go built-in append function, you must always keep the idea that “the array may have been changed” in mind, and be very careful about it, otherwise, it may bite you!
[Read More]