Myths of Golang

from rubyist perspective

Everything in go pass by value

Go variable without defining

  • if it is boolean it will be false by default
  • if it is string it will be empty string

Naked return

func Hello() (string) {
hello := "hello"
return hello
  • Naked return
func Hello() (hello string) {
hello = "hello"

Variable with Initializer

Short Variable Declaration

Multiple Variables

Type Conversion


Conditional Statements

  • for loop init and post expression is option, so it will become while loop technically
  • Variable can be declared in if statement, like ruby but its scope is within if statement
  • Switch without condition is like switch true



  • pointer passing to a function func foo(p *int){...}
  • print address: fmt.Println(p)
  • print value: fmt.Println(*p)
package mainimport (
func main() {
x := 1
var p *int
p = &x
fmt.Println("value:", x)
fmt.Println("address:", &x)
fmt.Println("address:", p)
func foo(x *int){
fmt.Println("print value inside function:", *x)
fmt.Println("print address inside function:", x)
}value: 1
address: 0xc000094010
address: 0xc000094010
print value inside function: 1
print address inside function: 0xc000094010

Pointer to Struct


  • To modify of the slice in the function, it need to pass slice pointer instead
  • nil slice is not empty slice. Empty slice can grow, but nil slice cannot grow
  • string is a readonly slice of bytes. This way of design, it is very efficient to have substring from a string. They both safely point to same array

Array and Slice

  • Slice is the reference to underlying array. If changes the slice value, the underlying value will change too
  • Slice or array value can be struct too
  • Slice length and capacity are different. Slice length is slice length. Slice capacity is underlying array length

CAP rule

  • slice from lower bound, capacity will be decrease
  • slice from upper bound, capacity will be the same
  • with append function, cap is not reliable. It depends on new allocated underlying array length. It could never be equal to length of slice


  • for with range, the first element is index of slice. The second element is value of slice


Function value



Pointer receiver

  • You want to modify receiver in the method
  • More efficient not to copy value on each method call in the case large struct

Function and Pointer

  • Function with value argument must take value otherwise will return error.

Method and Receiver


  • Interface value and type: when calling method, go handles null pointer exception gracefully when it has interface type but no value. However it will be run time error if it has no value and type when calling method.

Type assertion

Go Concurrency

  • Channels: what to be shared/accessed between routines
  • Channel Direction: the restriction of channel to send/receive value. By default channel has bi-direction. for example:
    restrict to only send value (write only): c chan<- int
    restrict to only receive value (read only): c <-chan int
    can both send/receive: c chan int
  • Select: switch case for channel
  • Buffered Channels: the channel capacity, max number of values that can be sent to channel

String vs []byte

  • []byte is mutable — more efficient, in-place modifying


Rune type




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store