• Home
  • About
    • Jinho's journal about software engineering photo

      Jinho's journal about software engineering

      Very simple writing about sofware engineering.

    • Learn More
    • LinkedIn
    • Github
    • StackOverflow
  • Posts
    • All Posts
    • All Tags
  • Projects

Several tips for Go coding-1

07 May 2019

Reading time ~2 minutes

Several tips for Go coding - 1

It’s summarized note that I need to remember for Golang.

Playground

We need to test our code snippet is working or not quickly. Use the playground https://play.golang.org/.

Structure

Declare the structure like this.

    //Due date can be nil because there is a task without a due date.
    type Task struct {
            title string
            done  bool
            due   *time.Time
    }
    
    myTask := Task{"homework", false, nil}
    
    var currentTime = time.Now()
    var yourTask = Task{"homework", false, &currentTime}
    
    var hisTask = Task{title: "cleanup_codes", done: false}

Enum

  • Use ‘enum’ when you wanna use bool. Because you’ll get so many incidents that you modify your code frequently as service is growing up.
  • We can change the task structure like following.
    • Use iota to represent enum type.
    type status int // How to define the value?
    
    const (
        UNKNOWN status = iota //<== will be 0
        TODO                  //<== will be 1
        DONE                  //<== will be 2
    )
    
    type Task struct {
        title  string
        status status // <== This
        due    *time.Time
    }
    
    myTask := Task{"homework", TODO, nil}
    
    var currentTime = time.Now()
    var yourTask = Task{"homework", DONE, &currentTime}
    
    var hisTask = Task{title: "cleanup_codes", status: UNKNOWN}
  • Effective Go introduces a very smart way to define the constant.
    type ByteSize float 64
    
    const (
        _ = iota // ignore first value
      KB ByteSize = 1 << (10 * iota) //2^10 = 1024 
      MB   // 2^20 = 1024*1024
      GB   // 2^30 = 1024*1024*1024
      .....
    )

Table-driven tests

To test many cases in batch, you can declare the table as test cases. And run the unit test like following.

This is the tip that you manage many test cases.


    package main
    
    import (
        "testing"
    )
    
    
    func fib(n int) int {
        if n == 0 {
            return 0
        } else if n == 1 {
            return 1
        } else {
            return fib(n-1) + fib(n-2)
        }
    }
    
    
    func TestFib(t *testing.T) {
        cases := []struct {
          in, want int     // Test case dat structure. 
        }{
                { 0, 0 },     // Set the test cases in the table.
                { 5, 5 },
                { 6, 8 },
        }
      for _, c := range cases {  // Travese table and run mutiple cases. 
        got := fib(c.in)
        if got != c.want {
        t.Errorf("Fib(%d) == %d, want %d", c.in, got, c.want)
        }
      }
    
    }

You can read more articles from this. It’s a very common case to implement many unit tests very effectively.



GoTips Share Tweet +1