Tag: golang

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 […]

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 { […]

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, […]

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 […]

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 […]

Why Using Golang sync Pool is a Bad Idea?

Not an absolutely bad idea, but you need a careful understanding before using sync Pool. Golang sync Pool is used to create a self-managed object pool, with just New/Get/Set functions. The Pool implementation uses a mutex based locking for thread-safe operations from multiple Go routines. Design Principles Golang […]