how to test the result in goroutine without wait in test
If you really want to check the result of a goroutine, you should use a channel like so:
package main
import (
"fmt"
)
func main() {
// in test
c := Hello()
if <-c != "done" {
fmt.Println("assert error")
}
// not want to check result
Hello()
}
func Hello() <-chan string {
c := make(chan string)
go func() {
fmt.Println("do something")
c <- "done"
}()
return c
}
https://play.golang.org/p/zUpNXg61Wn
Most questions of "How do I test X?" tend to boil down to X being too big.
In your case, the simplest solution is not to use goroutines in tests. Test each function in isolation. Change your code to:
func Hello() {
go updateDatabase()
doSomething()
}
func updateDatabase() {
// do something and store the result for example in db
}
func doSomething() {
// do something
}
Then write separate tests for updateDatabase
and doSomething
.