Slice append function in Go

Append an element to the slice:

nums := []int{1, 2, 3}
nums = append(nums, 4)

Append multiple elements to the slice:

nums := []int{1, 2}
nums = append(nums, 3, 4)

Append slice to another slice:

nums := []int{1, 2}
tens := []int{11, 12}
nums = append(nums, tens...)

// nums is {1, 2, 11, 12}
go  note 

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 

October and November 2020 PythonPune virtual meetups

I have been part of the PythonPune organizing team since May 2019. Organizing and attending PythonPune meetups always excite me because I get to learn something new and meet new people. Due to the COVID-19 pandemic situation, we moved meetups to virtual events since March'20, keeping the same level of excitement. The PythonPune team always prefers Free/Libre and Open Source Software (FLOSS) for everything. We use the Jitsi Meet platform for speakers and hosts, and live stream it on YouTube channel to reach our audience. [Read More]

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 

On Journaling

My notes on journaling from dailystoic.com. Writing reflects clear thinking and communication. Benefits of journaling Different studies noted following benefits: Journaling improves communication skills. Helps in sleep faster. Decreases emotional distress. Improves working memory. Cognition boost. Figure things out and clear your head. How to write journal Start small Write one line 5 minute journal Brainstorm Ideas in your journal Bullet Journal Quoted from the post: [Read More]