Post

GO Tutorial: Getting started

Go 를 처음 접하는 개발자의 시점에서 Basic syntax를 정리해보려 한다.

Packages and Imports

  • This code groups the imports into a parenthesized, factored import statement
  • In Go, a name is exported if it begins with a capital letter

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    package main
    
    import (
      "fmt"
      "math/rand"
    )
    
    func main() {
      fmt.Println("My favorite number is", rand.Intn(10))
    }
    

Functions

  • A function can take zero or more arguments
  • Each argument has a type beside or when more than one consecutive named function parameters share a type, omit the type from all but the declare at the end

    1
    2
    3
    
    func swap(x, y string) (string, string) {
      return y, x
    }
    
    • Multiple results: a function can return any number of results
    1
    2
    3
    4
    5
    
    func split(sum int) (x, y int) {
      x = sum * 4 / 9
      y = sum - x
      return
    }
    
    • Naked return: a return statement without arguments returns the named return values
    • defer: excute something after finish the function

Basic Types

typeotherszero valuesnote
bool false 
string ”” 
intint8 int16 int32 int640 
unintuint8 uint16 uint32 uint64 uintptr0 
byte  alias for uint8
rune  alias for int32
float32float640 
complex64complex128  

Variables and Constant

  • The var statement declares a list of variables

    1
    2
    3
    4
    
    var version float64 = 16.3
    // OR
    vers := 16.3 // inference type: float64
    const language string = "Golang"
    
    • Short variable declarations(:=): short assignment statement can be used in place of a var declaration with implicit type inside a function. Short variable declarations does not support outside of a function
    • Constants can be character, string, boolean, or numeric values
    • Constants cannot be declared using the := syntax.

for, range, …args

  • The init and post statements are optional
  • At that point you can drop the semicolons: C’s while is spelled for in Go

    1
    2
    3
    
    for i := 0; i < 10; i++ {
      sum += i
    }
    
    • If you omit the loop condition it loops forever, so an infinite loop is compactly expressed
    1
    2
    3
    
    for index, number := range numbers {
      fmt.Println(index, number)
    }
    
    • For looping through an array, range can be used. first return value is index, and second return value is value in the array[index]

if Statement

  • Like for, the if statement can start with a short statement to execute before the condition
  • Variables declared inside an if short statement are also available inside any of the else blocks

    1
    2
    3
    4
    5
    
    if v := math.Pow(x, n); v < lim {
    	return v
    } else {
    	fmt.Printf("%g >= %g\n", v, lim)
    }
    

Switch Statement

  • Go’s switch is like the one in C, C++, Java, JavaScript, and PHP, except that Go only runs the selected case, not all the cases that follow.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    today := time.Now().Weekday()
    switch time.Saturday {
    case today + 0:
      fmt.Println("Today.")
    case today + 1:
      fmt.Println("Tomorrow.")
    case today + 2:
      fmt.Println("In two days.")
    default:
      fmt.Println("Too far away.")
    }
    

Defer

  • A defer statement defers the execution of a function until the surrounding function returns.
  • Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    func main() {
      fmt.Println("counting")
    
      for i := 0; i < 10; i++ {
        defer fmt.Println(i)
      }
    
      fmt.Println("done")
    }
    

Reference

This post is licensed under CC BY 4.0 by the author.