×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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

This section contains the Golang Goroutine, Map, and Reflection find output programs (set 1) with their output and explanations.
Submitted by Nidhi, on November 09, 2021

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

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.


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

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.


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

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.


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

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.


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()

}

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.

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.