How to Use runtime.Gosched and runtime.Goexit

runtime.Gosched yields the processor to other goroutines, while runtime.Goexit terminates the current goroutine immediately.

Use runtime.Gosched() to yield the processor to other goroutines and runtime.Goexit() to terminate the current goroutine immediately without returning to the caller.

package main

import (
	"fmt"
	"runtime"
)

func main() {
	go func() {
		fmt.Println("Starting goroutine")
		runtime.Gosched() // Yield to other goroutines
		fmt.Println("Resumed")
	}()

	go func() {
		fmt.Println("Exiting goroutine")
		runtime.Goexit() // Terminate this goroutine
		fmt.Println("This will not print")
	}()

	// Wait for goroutines to finish
	for i := 0; i < 1000000; i {}
}