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()
}
Answer Program 1:
Output:
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:
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:
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:
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:
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