Categories
Go My Go Tutorial

Go Online Book (Free)

https://www.practical-go-lessons.com/

Categories
Go My Go Tutorial

Go (1.15.6 or later) Installation and Setup in Windows 10 (Fresh Install)

Official Go download page, this link gives you more option to choose from OS platform and OS architecture x86-64 build. And you can use the go build command parameter to building a binary for an application that can run for CPU of x86 (386) and x86-64.

https://golang.org/dl/

Install

Open the MSI file you downloaded and follow the prompts to install Go. The default folder used is C:/go. Or choose any folder that Windows 10 security may not ask for an administrator password for install programs/files using that folder path.

System Environment variable created by MSI installer

GOPATH C:\Users\loginname\go

Note: loginname is the user name to Window login

PATH C:\Go\bin

Verify

Launch a windows terminal, type go version

go version go1.15.6 windows/amd64 output confirmed go installation has been successful. Otherwise, use the Windows Control panel app -> Programs find go1.15.6 has been program has been installed.

Troubleshooting

Google, Go forum, and Stackoverflow for assistance.

https://forum.golangbridge.org/

https://stackoverflow.com/questions/tagged/go

Categories
Go My Go Tutorial

Range Over Slice and Map

package main

import "fmt"

func main() {
  kvs := map[string]string{"1":"shan kuo","2":"Hoi Leng","3":"Choi Yan","4":"Evelyn Chan"}
  for k, v := range kvs {
    fmt.Printf("%s -> %s\n",k,v)
  }
}

https://repl.it/join/gdzplzna-shankuo

 go build
 ./main
1 -> shan kuo
2 -> Hoi Leng
3 -> Choi Yan
4 -> Evelyn Chan

Categories
Go My Go Tutorial

Looping Over Arrays and Slices

package main

import "fmt"

func main() {
  names := []string{"Shank Kuo","Hoi Leng","Choi Yan","Evelyn Chan"}

  for i:=0; i < len(names); i++ {
    fmt.Println(names[i])
  }
}

https://repl.it/@shankuo/LoopArraySlice

go version go1.14 linux/amd64
 go run main.go
Shank Kuo
Hoi Leng
Choi Yan
Evelyn Chan

Categories
Go Testing

Go Built-in Unit Testing

Naming conventions ( prefix with underscore)

suffix with underscore with the filename. For example, the hello.go is the source file then hello.go with test package will have the filename hello_test.go tells the go test command that this file contains test functions.

hello_test.go

Testing package

Importing the testing package is a must to be included along with other imported packages such as the “fmt” package that it commonly in use.

import (
    "testing"
)

Test command

At the command line where the source code directory, run the go test command to execute the test

go test

Implement a Test Functions

Expect results of the function

Prefix the Test to the function name. For example, TestHelloName()

// TestHelloName calls greetings.Hello with a name, checking 
// for a valid return value.
func TestHelloName(t *testing.T) {
    name := "Gladys"
    want := regexp.MustCompile(`\b`+name+`\b`)
    msg, err := Hello("Gladys")
    if !want.MatchString(msg) || err != nil {
        t.Fatalf(`Hello("Gladys") = %q, %v, want match for %#q, nil`, msg, err, want)
    }
}

Resources

https://golang.org/doc/tutorial/add-a-test