go build <name_file>
./<exe_file>
go run <name_file>
-
have to use last name
_test
, for example:hello_world_test.go
-
the function firstname is
Test..
, for Example:TestHelloWorld()
-
add the parameter
t *testing.T
with no return value.
go test <path>
go test -v <path> // -v use to show the logs
go test -v -run=<name_function> <path> -count=1 // -count to ignore caching
/*
** Either run all tests with go test -v or specify the source files that your test depends on, e.g.
*/
go test double.go double_test.go
-
Parameter for unit testing
-
Fail()
failing the unit test but continue till finish. -
FailNow()
failing the unit test and stoppped at the same time. -
Error()
callFail()
with print an error as a logging. -
Fatal()
callFailNow()
with print an error as a logging.
- Parameter for testing in main
- Parameter for brenchmarking
- goroutine is pretty small, it work as concurrency instead of parallel.
- channel is an alternative to receive data by goroutine. it is being a tunnel to communicate with goroutine function. goroutine will be blocked untill the channel receive the data (blocking). The concept is similar with
async await
in javascript.
channel := make(chan int)
channel <- 19 // to assign the data
bufferData <- channel // to give the data
-
channel only buffer one data. If want more, need another goroutine to create
-
please close channel if it's not used due to memory leak.
-
by default, the parameter is parsing the value. It is different with channel. If parsing the channel, it is automatically passing the reference.
-
channel is possible to give a sign in and out while parsing as parameter.
chan<-
for in and<-chan
for out. -
Buffered Channel use to buffer data whenever didn't available or in some case to handle sender faster than receiver.
channel := make(chan string, 3) // create channel with 3 buffer long.