Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why golang running this code in wrong order?

When i run this code:

package main
import "fmt"

func main() {
    a := testfunc()
    b := testfunc()
    fmt.Println(a)
    fmt.Println(b)

}

func testfunc() string{
    fmt.Println("test")

    return "testtii"

}

Why output is:

test 
test  
testtii  
testtii 

Instead of

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

test  
testtii  
test  
testtii

>Solution :

func main() {
    a := testfunc()  // This will print "test", return "testii"
    b := testfunc()  // This will print "test", return "testii"
    fmt.Println(a)   // This will print "testii"
    fmt.Println(b)   // This will print "testii"

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading