调用函数返回的Goroutine终止

If I spawn a goroutine in a function and that function returns, will the goroutine terminate, or will it continue to execute?

You must have to wait for all goroutine(s) to be finished in your program main thread (or say in the main method).

Let take an example

package main

import (
    "fmt"
    "time"
)

func foo() {
    go func() {
        fmt.Println("sleeping for 5 sec")
        time.Sleep(5 * time.Second)
        fmt.Println("Done")
    }()
}

func main() {
    foo()
}

When you run above code this will exit immediately (means the main thread will not wait for goroutine to finish first).

To achieve this in go I am using https://golang.org/pkg/sync/#WaitGroup

package main

import (
    "fmt"
    "sync"
    "time"
)

// see https://golang.org/pkg/sync/#WaitGroup
var wg sync.WaitGroup

func foo() {
    go func() {
        wg.Add(1)
        defer wg.Done()

        fmt.Println("sleeping for 5 sec")
        time.Sleep(5 * time.Second)
        fmt.Println("Done")
    }()
}

func main() {
    foo()
    fmt.Println("Waiting for goroutine to finish")
    wg.Wait()
    fmt.Println("All goroutine to finished")
}