ezchi / learn-go-by-examples

My personal learning journey of golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

My journey of learning Go

This journey start with follow https://go.dev/doc/tutorial/.

Getting started

Hello World

Create the directory

mkdir hello

Enable dependency tracking for the code

To enable the tracking by create the go.mod file, run the go mod init command as below with example/hello as name of this example code.

go mod init example/hello

First code

The first hello world code is in hello/main.go

Run the first code with go run command

go run .

Call code in external package

This session will learn how to add external package as example and call code in that package.

Create a Go module

Start a module that others can use

Create greating directory

mkdir -p greetings

Initialize module

go mod init example.com/greetings

Add example code to greetings.go

Call code from another module

Add example code to hello/main.go

Replace module with local module

go mod edit -replace example.com/greetings=../greetings

Tidy go.mod

go mod tidy

Run the new code

go run .

Return and handle an error

Add return error to greetings.go

Add error handle to hello/main.go

Run the code

go run .

Return a random greeting

Add random greeting message to greetings/greetings.go

Run the code

go run .

Return greetings for multiple people

Add a test

Run test

go test -v

Compile and install the application

Compile the application

go build

Install the application

go install

About

My personal learning journey of golang

License:MIT License


Languages

Language:Go 100.0%