[Go] Execution / build / package test

Prerequisite environment

・ MacOS ・ Goenv 2.0.0beta11 ・ Go version go1.15.2 darwin / amd64

Execution method

This time we will run the classic Hello World.

For the time being, create a file with the name ** hello.go ** directly under $ GOPATH, and write the following program there.

package main  //Package declaration

import (  //Import the package to use

func main() {  //Function main(Entry point)Definition of
    fmt.Println("Hello World")  

Then open the directory where you created the file in the terminal, enter `` `$ go run hello.go```, and when ** Hello World ** is displayed, the execution is successful.


Next, compile hello.go into an executable file format.

You can specify the executable file name by entering the ** build command ** as shown below and using the -o option.

$ go build -o hello hello.go

Then, an executable file called ** hello ** will be created, so you can execute Hello World just by entering the following command in the terminal.

$ ./hello
Hello World

Package testing

Suppose you want to test the files under the tests directory, assuming that the package structure is as follows.

It also uses the Dependent Module Management Tool Modules,

go mod init github.com/username/It is a test project.

 * It has been imported from go 1.13, but it is in the transition period from go 1.11, but it can be used by setting ```export GO111MODULE = on```.

testproject │────── tests │ │────testA │ │────testB │ │────testC │
│────── main.go

 First, create a file under the tests directory so that it ends ** _test.go **. This is a set rule when testing a package.

#### **`Example)tests_test.go`**

 Write the contents of the tests_test.go file as shown below.

package tests

import ( "testing"


func TestJapaneseSubject(t *testing.T) { expect := "Japanese" actual := JapaneseSubject()

if expect != actual {
	t.Errorf("%s != %s", expect, actual)


func TestEnglishSubject(t *testing.T) { expect := "English" actual := EnglishSubject()

if expect != actual {
	t.Errorf("%s != %s", expect, actual)


func TestMathSubject(t *testing.T) { expect := "Math" actual := MathSubject()

if expect != actual {
	t.Errorf("%s != %s", expect, actual)


 Then, enter the command in the terminal, execute the test, and if the output is as follows, it is successful.

$ go test github.com/username/testproject/tests ok github.com/noa-1129/testproject/tests 0.506s

 You can also check the details for each file by adding the -v option.

$ go test -v github.com/username/testproject/tests === RUN TestJapaneseSubject --- PASS: TestJapaneseSubject (0.00s) === RUN TestEnglishSubject --- PASS: TestEnglishSubject (0.00s) === RUN TestMathSubject --- PASS: TestMathSubject (0.00s) PASS ok github.com/username/testproject/tests 0.230s

## Finally
 In this package test, I used ** Go Modules ** as a dependent module management tool, but I will write about Go modules in the next article!

Recommended Posts

[Go] Execution / build / package test
Execution environment test after Go language installation
Go language-standard package
Go language-rand package
Python test package memo
Environment construction, Build -Go edition-
Bayesian network package ~ Pebl tutorial execution ~
Build a go environment using Docker
What's happening when you "go build"?
Build PyPy execution environment with Docker