Constants in Go

Intro Constants are declared like variables, but with the const keyword. Constants can only be character, string, boolean, or numeric values and cannot be declared using the := syntax. An untyped constant takes the type needed by its context. Example: const Pi = 3.14 const ( StatusOK = 200 StatusCreated = 201 StatusAccepted = 202 StatusNonAuthoritativeInfo = 203 StatusNoContent = 204 StatusResetContent = 205 StatusPartialContent = 206 ) Constants belong to compile time. [Read More]

Conversion between array and slice

In Go, array is a fixed length of continuous memory with specified type, while slice is just a reference which points to an underlying array. Since they are different types, they can’t assign value each other directly. See the following example: package main import "fmt" func main() { s := []int{1, 2, 3} var a [3]int fmt.Println(copy(a, s)) } Because copy only accepts slice argument, we can use the [:] to create a slice from array. [Read More]

Create greet package

mkdir src/greet Then create a new Go source code file (greet.go) in src/greet: # cat src/greet/greet.go package greet import "fmt" func Greet() { fmt.Println("नमस्ते किंशुक!") } You can consider this greet directory provides a greet package which can be used by other programs. (3) Create another package hello which utilizes the greet package: # mkdir src/hello # cat src/hello/hello.go package main import "greet" func main() { greet.Greet() } You can see in hello. [Read More]

Debugging

No one can write bug-free code, so debugging is a requisite skill for every software engineer. Here are some tips about debugging Go programs: (1) Print Yes! Printing logs seems the easiest method, but it is indeed the most effective approach in most cases. Go has provided a big family of printing functions in fmt package, and using them neatly is an expertise you should grasp. (2) Debugger In some scenarios, maybe you need the specialized debugger tools to help you spot the root cause. [Read More]

Decorate types to implement io.Reader interface

The io package has provided a bunch of handy read functions and methods, but unfortunately, they all require the arguments satisfy io.Reader interface. See the following example: package main import ( "fmt" "io" ) func main() { s := "Hello, world!" p := make([]byte, len(s)) if _, err := io.ReadFull(s, p); err != nil { fmt.Println(err) } else { fmt.Printf("%s\n", p) } } Compile above program and an error is generated: [Read More]

Dependency Management - vendoring

Google is well known to have an immense mono repo so go get works well for them. This is not the case with everyone else. By default go get pulls the master branch of the repo you point it at. When you do a go get it pulls in the required dependencies, this means there are issues with reproducibility. As of go 1.5 they looked to address some of the issues by introducing the vendor directory. [Read More]
get 

Error handling intro

nil is a predeclared identifier like true, false, len(), int32, float64 etc. Since it is a predeclared identifier, it can be used anywhere without importing any package. nil value means that the value is not initialized yet. It is similar to following identifiers in other languages: null // JavaScript None // Python null // Java nil // Ruby The zero value of all pointer-based types in Go is nil. Following are the pointer-based types in Go: [Read More]

Error handling intro

Error is by far the most used interface in Go. lets have a look at it: type error interface { Error() string } //The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error. So any type that implements the Error method is by default an error type. Some Examples: package main import "fmt" type MyError string func (me MyError) Error() string{ return string(me) //cast it back to string } type MyOtherError struct{ Code int Message string } func (me MyOtherError) Error() string{ return fmt. [Read More]

error vs errors

Handling errors is a crucial part of writing robust programs. When scanning the Go packages, it is not rare to see APIs which have multiple return values with an error among them. For example: func Open(name string) (*File, error) Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. If there is an error, it will be of type *PathError. [Read More]

Exporting name

To export a name in Go, just make it’s first letter an uppercase letter. From within a package namespace you can refer to private functions and variables (starting with lower case) but from outside the package, you can only access the things exported from that package. Think public and private key word in Java.

//exported 

func MyPublicFunc(){

}

//private 
func myPrivateFunc(){

}