Skip to content

Go Language Basics

Posted on:August 19, 2023 at 10:20 PM

1 Grammer

1.1 Program Structure

package main //current package name

import "fmt" // import

import (
	"pkg1"
	"pkg2"
)            // group import

func main() {
   fmt.Println("Hello, World!")
}

1.2 Basic Syntax

https://www.tutorialspoint.com/go/go_program_structure.htm

Data Type

Variable

Const

Operators

Pointer

Structure

```go
type Books struct {
   title string
   author string
   subject string
   book_id int
}
```

1.3 Higher level Syntax

https://www.tutorialspoint.com/go/go_slice.htm

2 CLI

go run hello.go

3 Project Layout

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

3.1 Go module management

official guidelines: https://golang.org/ref/mod

refs: [https://faun.pub/understanding-go-mod-and-go-sum-5fd7ec9bcc34](https://faun.pub/understanding-go-mod-and-go-sum-5fd7ec9bcc34)

go.mod file defines all dependent packages in the project. It is placed at the root directory of the go project.

3.1.1 go mod init [package_name]

3.1.2 go get [package_name]

3.1.3 go mod tidy

3.1.4 go install

3.1.5 go clean -modcache

3.1.6 go build

3.2 Go access ability

go.mod issue that current workspace is a package.

Assume it has the following structure:

---mypackage
 |---pkg
   |---trd_package
     |---trg_package.go
 |---main.go
 |---go.mod
 |---go.sum

// trd_package.go
package tpkg

// go.mod
module xxx.com/abc/def

// main
package mypackage
import "xxx.com/abc/edf/pkg/trd_package"

func main() {
	tpkg.xxx
}