Printf and Sprintf formatting

Following formatting can be used with fmt.Printf as well as fmt.Sprintf: // String and slice of bytes %s // the uninte­rpreted 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]

Slice vs Arrays

Performance Slice operations are cheap! Slicing: Creates a new slice header. Assigning a Slice to another Slice or, passing it to a function: Only copies the slice header. Slice header has a fixed size and it doesn't change even if we have got millions of elements. Array can be expensive as compared to Slice. Assigning an array to another array or passing it to a function: Copies all the elements of it. [Read More]

Type assertion and type switch

If we have a value and want to convert it to another or a specific type (in case of interface{}), we can use type assertion. A type assertion takes a value and tries to create another version in the specified explicit type. This is how we can use type assertion: x.(T) x is the variable whose type must be interface, and T is the type which you want to check. For example: [Read More]

Zero Values

When a variable is declared and it isn’t assigned any value at the time of declaration, Go will assign a zero value to it based on it’s variable type. Type of a variable decides what a zero value to it will take initially when declared (and if it isn’t assigned any value at the time of declaration). // Zero Values assigned to variables by Go when they are declared and not assigned any values at the time of declaration. [Read More]