Keyed array elements in Go

Unkeyed array elements: example := [3]float64{ 1.3, 6.7, 4.0, } Keyed array elements: example := [3]float64{ 0: 1.3, 1: 6.7, 2: 4.0, } Keyed array in different order: example := [3]float64{ 1: 1.3, 0: 6.7, 2: 4.0, } Auto-initialize keyed array elements: example := [...]float64{ // index 0 is empty // index 1 is empty 2: 4.0, // index 2 } Unkeyed and keyed array elements: example := [. [Read More]
go  note 

break and continue labels

break and continue statements can be use with label in Go, similar to goto statement. It is optional. Scope of labels are limited to the function and does not conflict with variable name, as it live in separate space. break and continue with label is only used in for, switch and select statements. OuterLoop: for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt. [Read More]
go  note 

Go Switch fallthrough

Switch cases in go can use fallthrough keyword to fall into next case without checking it’s condition if the current case has fallthrough statement at the end. For example: i := 142 switch { case i > 100: fmt.Print("big ") fallthrough case i > 0: fmt.Print("positive ") fallthrough default: fmt.Print("number") } The output is, big positive number “fallthrough” can be used in scenario where the block of both cases are same. [Read More]
go  note 

iota constant in Go

Linearly increasing or decreasing constant values can be declared using “iota”. For example: func main() { const ( monday = 0 tuesday = 1 wednesday = 2 thursday = 3 friday = 4 saturday = 5 sunday = 6 ) } This can be simplified as: func main() { const ( monday = iota // = 0 tuesday // = 1 wednesday // = 2 thursday // = 3 friday // = 4 saturday // = 5 sunday // = 6 ) } iota can be used in expression. [Read More]
go  note 

Repeating constant with same value

If multiple constants have same value.

	const (
		min int = 1
		max int = 1
	)

Then the constant declaration can be simplified as below:

	// constants repeat the previous type and expression
	const (
		min int = 1
		max     // int = 1
	)
}
go  note