Q:

Golang Goroutine, Map, and Reflection | Find Output Programs | Set 2

0

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")
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

Hello India
Hello World
Hello India
Hello World
Hello World
Hello India
Hello World
Hello India

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:

Hello World
0
Hello India
1
Hello World
Hello India
Hello World
2
Hello India

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:

Hello World
Hello India
0
Hello India
1
Hello World
2
Hello India
Hello World

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:

./prog.go:7:3: undefined: time
./prog.go:17:4: undefined: time
./prog.go:22:2: undefined: time

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:

Hello India
Hello India
Hello India

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now