Please correct me if I am wrong. As long as I know, goroutine works roughly similar to threads. So If I spawn the same function with different parameters prefixing with go. It should work perfectly fine ?
package main
import "fmt"
func f(from string) {
for i := 0; i < 3; i++ {
fmt.Println(from, ":", i)
}
}
func main() {
go f("direct")
go f("redirect")
//var input string
//fmt.Scanln(&input)
}
Actual Output:
rahul@g3ck0:~/programs/go$ go run goroutine.go
rahul@g3ck0:~/programs/go$
I just get back prompt.
Expected output:
direct : 0
direct : 1
direct : 2
redirect : 0
redirect : 1
redirect : 2
Not necessarily in the same order.
Not able to understand this strange behaviour. Am I missing something ?
EDIT: Adding a Scan statement resolves it. But is there any better way of doing it ?