Running the following snippet:
package main import ( "context" "fmt" "log" "time" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type User struct { ID string Name string Followers map[string]*User Friends map[string]*User } func main() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017")) err = client.Ping(ctx, nil) if err != nil { log.Fatal("could not connect") } coll := client.Database("testing").Collection("users") ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) defer cancel() u1 := &User{ID: "24", Name: "Test1", Followers: map[string]*User{}, Friends: map[string]*User{}} u2 := &User{ID: "25", Name: "Test2", Followers: map[string]*User{}, Friends: map[string]*User{}} u1.Friends[u2.ID] = u2 u2.Followers[u1.ID] = u1 // uncommenting the next line and commenting out the two above gives the same behavior i.e. assigning to self //u1.Followers[u1.ID] = u1 res, err := coll.InsertOne(ctx, u1) if err != nil { fmt.Println("error:", err) } fmt.Println(res.InsertedID) }
gives the following error:
runtime: goroutine stack exceeds 1000000000-byte limitruntime: goroutine stack exceeds 1000000000-byte limitfatal error: stack overflow