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 

My first golang package

Here is my first golang package, https://github.com/akshay196/golang. It is pretty simple package that only contains a function Version(). The function returns go version string.

How to use?

Run command:

$ go get github.com/akshay196/golang

Then use package in your go code:

package main

import (
    "fmt"
    "github.com/akshay196/golang"
)

func main() {
    fmt.Println(golang.Version())
}

This is done as part of exercise in learngo.

go 

Run multiple go files in a same package

Suppose we have multiple go files, for example greet.go, bye.go and main.go in same directory and all have same package name. The main.go is calling greet() function from greet.go, similary greet.go is calling bye() from bye.go. In this case how to run this, so that it successfully call functions from other go files. Simply run go run *.go or go build. go build will generate binary with name similar to the directory that contains files. [Read More]
go