I am new to Go. My question is what's the difference to run a function with or without Go. For example in a .go file I have one test() function, when I call this function what's the difference for "test()" and "go test()".
test()
will run when you call it. go test()
will run asynchronously on its own completely independent of test()
.
If you have a program like this:
func main() {
test("bob")
go test("sue")
}
func test(msg string) {
fmt.Printf("hello %v", msg)
}
You will only see output
hello bob
since the main
function execution jumps right through to the end. There is nothing waiting for go test("sue")
to complete since it's its own independent function.
You can block for go test("sue")
by putting in a time.Sleep
or a command line input with fmt.Scanln(&input)