development

go tool: no such tool “compile”

I started facing go tool: no such tool “compile” error after installing a local build of go. The golang source was in /usr/local $ cd /usr/local $ ls total 41240 drwxr-xr-x@ 19 root wheel 608B Oct 18 19:35 go drwxr-xr-x 16 root wheel 512B Oct 18 19:35 . -rw-r–r–@ 1 root wheel 20M Oct 18 […]

Read more
golang, programming

Untyped Constants in Golang

Golang is extremely explicit with types. It supports constants of a type. But how about a const 2 or hello. What type would they assume!? Go solved this problem with untyped constants. Untyped means: A constant’s type depends on the type of assignee variable. The assignee’s type must have compatibility to hold constant’s value. Example […]

Read more
development

Notes on Go Structure Tags

Go has a feature to create a user-defined type with struct. Go also allows the program to specify meta-information with structure field. This meta-information is called Tags. A tag usually helps in packaging/unpacking a user-defined type structure. package main import ( “encoding/json” “fmt” ) type Test struct { // json tags indicate the key name […]

Read more
development

Golang: HTTP Client & Server with Query Params

Client func TestDelete(id string, registered bool) { path := “http://localhost:8085/v1/shops” // Create client client := &http.Client{} // Create request req, err := http.NewRequest(“DELETE”, path, nil) if err != nil { fmt.Println(err) return } q := req.URL.Query() q.Add(“id”, r.UID) q.Add(“registered”, strconv.FormatBool(registered)) req.URL.RawQuery = q.Encode() fmt.Println(req.URL.String()) // Fetch Request resp, err := client.Do(req) if err != nil […]

Read more
development

Golang: Switch-case does not fall through!

Unlike C/C++ switch-case statements, Golang switch case does not fall through. package main import ( “fmt” ) func main() { fmt.Print(“Go runs on “) os := “darwin” switch os { case “darwin”: case “linux”: fmt.Println(“Linux.”) default: // freebsd, openbsd, // plan9, windows… fmt.Printf(“%s.\n”, os) } } The above code does not print anything! package main […]

Read more
development

Golang Type Assertions: Dynamic Type Checking

Type Assertions Go supports dynamic type checking of variables. If the type of a variable is unknown till run-time, type assertions can run a validity check. // getRequest definition struct getRequest { name string } // Declare a void (anonymous) type variable request interface{} // Checking if variable request is not nil && // has […]

Read more