Q:

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

0

This section contains the Golang Goroutine, Map, and Reflection find output programs (set 1) 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("\n%s", msg)
    }
}

func main() {
    goroutine ShowMsg("Hello World")
    ShowMsg("Hello India")
}

Program 2:

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 ShowMsg("Hello World")
	ShowMsg("Hello India")
}

Program 3:

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() {
	ShowMsg("Hello India")
	go ShowMsg("Hello World")
}

Program 4:

package main

import "fmt"
import "time"

func PrintNum() {
	for i := 0; i < 4; i++ {
		time.Sleep(1 * time.Second)
		fmt.Printf("%d\n", i)
	}
}

func ShowMsg(msg string) {
	for i := 0; i < 4; i++ {
		time.Sleep(1 * time.Second)
		fmt.Printf("%s\n", msg)
	}
}

func main() {
	PrintNum()
	go ShowMsg("Hello World")
}

Program 5:

package main

import "fmt"
import "time"

func PrintNum() {
	for i := 0; i < 4; i++ {
		time.Sleep(1 * time.Second)
		fmt.Printf("%d\n", i)
	}
}

func ShowMsg(msg string) {
	for i := 0; i < 4; i++ {
		time.Sleep(1 * time.Second)
		fmt.Printf("%s\n", msg)
	}
}

func main() {
	go ShowMsg("Hello World")
	PrintNum()

}

All Answers

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

Answer Program 1:

Output:

./prog.go:14:15: syntax error: unexpected ShowMsg at end of statement

Explanation:

The above program will generate a syntax error because "goroutine" is not a valid keyword. Here, we need to use the "go" keyword instead of "goroutine" in our program.


 

Answer Program 2:

Output:

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

Explanation:

In the above program, we created the function ShowMsg() to print a specified message. Here, we called the function ShowMsg() using the "go" keyword to execute parallelly.


 

Answer Program 3:

Output:

Hello India
Hello India
Hello India
Hello India

Explanation:

In the above program, we created the function ShowMsg() to print a specified message. Here, we called function ShowMsg() at last with the "go" keyword. Then it will not execute parallelly.


 

Answer Program 4:

Output:

0
1
2
3

Explanation:

In the above program, we created two functions PrintNum() and ShowMsg(). Here, we called function ShowMsg() at last with the "go" keyword. Then it will not execute. And, we need to call GO-ROUTINE before another function.


 

Answer Program 5:

Output:

Hello World
0
1
Hello World
2
Hello World
3

Explanation:

 

In the above program, we created two functions PrintNum() and ShowMsg(). Here, we called GOROUTINE with another function parallelly using the "go" keyword.

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