基礎實戰

Section 1

Section 1-1

# Create your project folder and cd inside
$ mkdir -p $GOPATH/src/github.com/username/project && cd "$_"
$ export GO111MODULE=on
$ go mod init github.com/appleboy/project

$ go mod download
$ go clean -i -x -modcache

section 1-2 govendor

$ mkdir -p $GOPATH/src/github.com/JasonYCHuang/project && cd "$_"

$ govendor init

$ govendor fetch github.com/gin-gonic/gin@v1.3

$ govendor sync

$ govendor add +external

Section 3

Section 3-1

  • 同一目錄下, 不含子目錄, 所有.go檔案的package要一樣

  • subpackage: package name = folder name; 則以下code使用

import (
    "github.com/user/projectname/subpkgname"
)
  • 只使用init, 則以下code使用

import (
    _ "github.com/user/projectname/subpkgname"
)

Section 3-2

global variable

const (
    one = 1
    two = 2
)

var (
    bar int
    foo string = "Hello"
)

func main() {
    // global variable
    bar = 3

    // local variable
    local := 3.4
}

Section 3-3

func return func

func foo() func() int {
    return func() int {
        return 1
    }
}

func as variable

bar := func() {
    fmt.Println("Hello")
}

bar()

anonymous

func() {
    fmt.Println("Hello")
}()

go func

go func(i, j int) {
    fmt.Println(i + j)
}(1, 2)

Section 3-4

func 參數是slice => call by reference

immutable: append(slice, element)

Last updated

Was this helpful?