Resolving conflicts with goroutines?

Issue

I have a really minor doubt
Suppose there are three func A,B,C
C is being called from both A and B
I am running A and B on different threads , will it result in conflict any time in feature when it calls C within it

for reference i am adding this code

package main

import (
"fmt"
)

func xyz() {
     for true {
         fmt.Println("Inside xyz")
         call("xyz")
     }
}

func abc() {
    for true {
         fmt.Println("Inside abc")
         call("abc")
    }
}

func call(s string) {
    fmt.Println("call from " + s)
}

func main() {
    go xyz()
    go abc()
    var input string
    fmt.Scanln(&input)
}

Here A = xyz(), B = abc(), C = call()

will there be any conflict or any runtime error in future while running these two go routines

Solution

Whether multiple goroutines are safe to run concurrently or not comes down to whether they share data without synchronization. In this example, both abc and xyz print to stdout using fmt.Println, and call the same routine, call, which prints to stdout using fmt.Println. Since fmt.Println doesn’t use synchronization when printing to stdout, the answer is no, this program is not safe.

Answered By – janos

Answer Checked By – Marie Seifert (GoLangFix Admin)

Leave a Reply

Your email address will not be published.