Golang many to many mysql

Hello, I need help about inserting dat to joining table let’s say I have models like this:

—models.go—

type Programmer struct{
Id int
Name string
}

type Project struct{
Id int
Name string
}

type assignment struct{
Project_id int
Programmer_id int
Name string
}

How I can insert new project and existing programmer data to assignment and also insert assignment name at the same time.

Thanks

PS: I’m using phone to post this question, please pardon of any typos

Hi @drafdev, You can use Embedded type in struct.
Here’s the link also for your reference. https://golang.org/doc/effective_go.html#embedding
Hope it fits to your question, because you mentioned about mysql. :slight_smile:

type Programmer struct{
  Id int
  Name string
}

type Project struct{
  Id int
  Name string
}

type assignment struct{
  Project_id int
  Programmer_id int
  Name string
 }

type Dev struct {
  Programmer
  Project
  assignment
}

func main() {
  prog := Programmer{32,"user1"}
  proj := Project{22,"Test project"}
  assg := assignment{1,2,"assignment 1"}

  user := &Dev{prog,proj,assg}

  fmt.Println(user)
  // Output:
  // &{{32 user1} {22 Test project} {1 2 assignment 1}}
}
1 Like

Yes, thank you :wink:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.