ADVERTISEMENT
ADVERTISEMENT

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

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

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.


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

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.


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

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.


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

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"

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

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.

Golang Find Output Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.