This section contains the Golang Goroutine, Map, and Reflection find output programs (set 2) with their output and explanations.
Program 1:
package main
import "fmt"
import "time"
func ShowMsg(msg string) {
for i := 0; i < 4; i++ {
time.Sleep(1 * time.Second)
fmt.Printf("%s\n", msg)
}
}
func main() {
go func() {
for i := 0; i < 4; i++ {
time.Sleep(1 * time.Second)
fmt.Println("Hello World")
}
}()
ShowMsg("Hello India")
}
Program 2:
package main
import "fmt"
import "time"
func PrintNum() {
for i := 0; i < 3; i++ {
time.Sleep(500 * time.Millisecond)
fmt.Printf("%d\n", i)
}
}
func ShowMsg(msg string) {
for i := 0; i < 3; i++ {
time.Sleep(500 * time.Millisecond)
fmt.Printf("%s\n", msg)
}
}
func main() {
go ShowMsg("Hello India")
go PrintNum()
go ShowMsg("Hello World")
time.Sleep(5000 * time.Millisecond)
}
Program 3:
package main
import "fmt"
import "time"
func ShowMsg(msg string) {
for i := 0; i < 3; i++ {
time.Sleep(500 * time.Millisecond)
fmt.Printf("%s\n", msg)
}
}
func main() {
go ShowMsg("Hello India")
go func() {
for i := 0; i < 3; i++ {
time.Sleep(500 * time.Millisecond)
fmt.Printf("%d\n", i)
}
}()
go ShowMsg("Hello World")
time.Sleep(5000 * time.Millisecond)
}
Program 4:
package main
import "fmt"
func ShowMsg(msg string) {
for i := 0; i < 3; i++ {
time.Sleep(500 * time.Millisecond)
fmt.Printf("%s\n", msg)
}
}
func main() {
ShowMsg("Hello India")
go func() {
for i := 0; i < 3; i++ {
time.Sleep(500 * time.Millisecond)
fmt.Printf("%d\n", i)
}
}()
time.Sleep(5000 * time.Millisecond)
}
Program 5:
package main
import "fmt"
import "time"
func ShowMsg(msg string) {
for i := 0; i < 3; i++ {
fmt.Printf("%s\n", msg)
}
}
func main() {
go func() {
for i := 0; i < 3; i++ {
time.Sleep(500 * time.Millisecond)
fmt.Printf("%d\n", i)
}
}()
ShowMsg("Hello India")
}
Answer Program 1:
Output:
Explanation:
In the above program, we created a function ShowMsg() and an anonymous GOROUTINE inside the main() function. In the main() function we called ShowMsg() with GOROUTINE and execute them parallelly.
Answer Program 2:
Output:
Explanation:
In the above program, we created two functions ShowMsg() and PrintNum(). Then we called both functions in the main() function using the "go" keyword for parallel execution.
Answer Program 3:
Output:
Explanation:
In the above program, we created the function ShowMsg(). Here, we also created an anonymous GOROUTINE inside the main() function. Then we called anonymous GOROUTINE with ShowMsg() as a GOROUTINE for parallel execution.
Answer Program 4:
Output:
Explanation:
The above program will generate syntax errors because we did not import the "time" package into our program. To import the "time" package, we need to use the below statement,
import "time"
Answer Program 5:
Output:
Explanation:
In the above program, we created a function ShowMsg() to print the specified message on the console screen 3 times, and we also created an anonymous GOROUTINE inside the main() function. And, we did not put a sleep() function inside the ShowMsg() that's why execution finished without executing anonymous GOROUTINE.
need an explanation for this answer? contact us directly to get an explanation for this answer