编辑代码

package main
import (
    "fmt"
    "context"
    "time"    
)
func main () {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
    fmt.Println("Hello world!   -  go.jsrun.net ")
    ctx, cancel := context.WithCancel(context.Background())

    go func(ctx context.Context){
        for{
            select{
                case <-ctx.Done():
                fmt.Println("goroutine was canceled...")
                return
                default:
                fmt.Println("goroutine exec...")
                time.Sleep(1 * time.Second)
            }
        }
    }(ctx)

    time.Sleep(3 * time.Second)

    fmt.Println("goroutine will be canceled...")
    cancel()

    time.Sleep(1 * time.Second)
    fmt.Println("main over...")
}