Environmental Science
Workspace:
A workspace is a directory corresponding to a specific project
src store source code
pkg code package
bin executable
Bag:
A code set that can call functions in a package by using the package name. Function name.
Test case:
Writing format: Filename: _test.go end Guide Package: testing Test function parameter (t *testing.T)
The test case will execute all functions under the current package and print the error information with t.Error.
go test will not print success information by default. We can use the - v parameter to display details.
package gotest import ( "errors" ) func Division(a, b float64) (float64, error) { if b == 0 { return 0, errors.New("Divisor cannot be 0") } return a / b, nil }
// Go test'u test.go'must end with'u test.go ' package gotest import "testing" func Test_Division_1(t *testing.T) { if i, e := Division(6, 2); i != 3 || e != nil { //try a unit test on function t.Error("Division function test failed") // If it's not as expected, report an error. } else { t.Log("The first test passed") //Record some information you want to record } } func Test_Division_2(t *testing.T) { t.Error("It just doesn't work.") } func Test_Division_3(t *testing.T) { if _, e := Division(6, 0); e == nil { //try a unit test on function t.Error("Division did not work as expected.") // If it's not as expected, report an error. } else { t.Log("one test passed.", e) //Record some information you want to record } }
$ go test -v === RUN Test_Division_1 --- PASS: Test_Division_1 (0.00s) gotest_test.go:9: The first test passed === RUN Test_Division_2 --- FAIL: Test_Division_2 (0.00s) gotest_test.go:14: It just doesn't work. === RUN Test_Division_3 --- PASS: Test_Division_3 (0.00s) gotest_test.go:21: one test passed. Divisor cannot be0 FAIL exit status 1 FAIL target/1.Test case 0.002s
GOPATH settings:
window: setting in environment variable
linux: set up the. bashrc file (user). profile (system)
Generally, you can set the under the user directory or under the root user.
#Put it at the end of the file, not in if export GOPATH="/home/cz/go" export PATH="$PATH:$GOPATH/bin"
Compile run program:
build Compile packages and dependency Libraries clean Delete object files and cached files doc Document showing package or symbol env promise Go environmental information bug start-up bug Presentation fix Update package to use new APIs fmt gofmt (Reformat) Package source code generate By processing source code generation Go file get Download and install packages and dependencies install Compile and install packages and dependencies list list packages run Compile and run Go program test Test package tool Run the specifiedgotool version Printing Go Version information vet Report possible errors in the package