Learn Go - duongphuhiep/blog1 GitHub Wiki

DI in GO

https://github.com/duongphuhiep/go-learn/blob/main/try-uberfx/README.md

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/

Redacting sensitive log data

https://betterstack.com/community/guides/logging/logging-in-go/#hiding-sensitive-fields-with-the-logvaluer-interface