I'm trying to get into go and I'm facing a problem which appears when using go routines on a method of a struct. What I was expecting is that the code prints the following output:
Item 1 was asked if it's alive
Item 2 was asked if it's alive
But it's not printing anything. When I leave out the "go"routines (at struct1.isAlive()) it's working fine. How can I make the goroutines work?
package main
import (
"fmt"
)
type somestruct struct {
ID int
ItemName string
}
func (s *somestruct) isAlive() (alive bool) {
alive = true
fmt.Printf("%s was asked if it's alive \n", s.ItemName)
return
}
func main() {
struct1 := somestruct{
ID:1,
ItemName:"Item 1"}
struct2 := somestruct{
ID:2,
ItemName:"Item 2"}
go struct1.isAlive()
go struct2.isAlive()