Skip to content

Learn Go

DUONG Phu-Hiep edited this page Apr 30, 2024 · 6 revisions

DI in GO

https://uber-go.github.io/fx/get-started/

  • Question: objects life cycle (scoped, singleton..)?

Go channel

Why do we need nil channel?

  • Read/Write nil channel blocked forever
  • Read a closed channel returns 0 (immediately)
  • Write to a closed channel panic
  • nil channel will never be selected (select case statement)
  • a closed channel, will be selected immediately, and get nil value of the channel type. Thus may cause the other channels in the select never get selected.

Use structured concurency

https://github.com/sourcegraph/conc

Guarantee that a struct implements interfaces as in C#

type SomeInterface interface {  
    Method()  
}
type Implementation struct{}

func (*Implementation) Method() { fmt.Println("Hello, World!") }

var _ SomeInterface = (*Implementation)(nil) // won't compile if missing implementation

Test cases

package main
import "testing"

func TestToSnakeCase(t *testing.T) {  
    type testCase struct {  
        description string  
        input       string  
        expected    string  
    }

    testCases := []testCase{  
        {  
            description: "empty string",  
            input:       "",  
            expected:    "",  
        }
    }

    for _, tc := range testCases {  
        t.Run(tc.description, func(t *testing.T) {  
            actual := ToSnakeCase(tc.input)  
            if actual != tc.expected {  
                t.Errorf("expected %s, got %s", tc.expected, actual)  
            }  
        })  
    }  
}

Projects layout

https://github.com/golang-standards/project-layout/