patterngoMajor
Why use the `go` keyword when calling a function?
Viewed 0 times
functionwhyusethecallingwhenkeyword
Problem
I was going through an example of a TCP server. They defined a function and called it with:
I thought it was weird seeing the
To my surprise, this worked!
go handleRequest(conn)I thought it was weird seeing the
go keyword, so I tried it without:handleRequest(conn)To my surprise, this worked!
- If both work the same way, why use the
gokeyword at all?
- If they work differently, what is the difference?
- Is there a certain style guideline to use, or should you just use personal preference?
Solution
go starts a goroutine, which is managed by golang run-time.It can either run on the current OS thread, or it can run on a different OS thread automatically.
You can refer to basic
golang documents for this, for example, one item in Google search keyword goroutine is golang concurrency.Context
Stack Overflow Q#26006856, score: 92
Revisions (0)
No revisions yet.