Golang: switch statement with fallthrough

Go only runs the selected case, not all the cases that follow. In effect, the break statement that is needed at the end of each case in those languages is provided automatically in Go.

package main

import (

func main() {
	var age int = 29
	switch {
	case age < 25:
		fmt.Println("age is less than 25")
	case age < 30:
		fmt.Println("age is less than 30")
	case age < 35:
		fmt.Println("age is less than 35")
	case age < 40:
		fmt.Println("age is less than 40")


age is less than 30
age is less than 35

使用上請注意不能在最後一個 case 使用 fallthrough 不然會發生錯誤。

cannot fallthrough final case in switch
Continue Reading

Golang: using pointers in for-range loop

You can achieve the same effect without a function though: just create a local copy and use the address of that:

for _, each := range arr {
    each2 := each
    err := ds.repo.Insert(&each2)
    // ...

Also note that you may also store the address of the slice elements:

for i := range arr {
    err := ds.repo.Insert(&arr[i])
    // ...

Solve “using a reference for the variable on range scope `xxxx` (scopelint)” error.

Continue Reading

Golang: convert between int, int64 and string

int to string

s := strconv.Itoa(123)

int64 to string

var n int64 = 123
s := strconv.FormatInt(n, 10) // s == "123" (decimal)
var n int64 = 123
s := strconv.FormatInt(n, 16) // s == "7b" (hexadecimal)

string to int

s := "123"
n, err := strconv.Atoi(s)

string to int64

s := "123"
n, err := strconv.ParseInt(s, 10, 64)
Continue Reading

basic Git commands

  • git clone [url]
  • git add [filename]
  • git commit -m “Commit message”
  • git diff [first branch] [second branch]
  • git reset [file]
  • git status
  • git remote prune [remote name]

use git cherry pick

  1. git show [commit hash]
  2. git cherry-pick [commit hash] -n or git cherry-pick [from commit hash]^..[to commit hash] -n
  3. git status
  4. git commit -m “`git log –oneline -n1 [commit hash]`”


git log --oneline [filename]
git checkout [commit hash] [filename]

Continue Reading