Mysql + golang question

I have worked with MySQL and made queries that contain sub queries in them in the past. I am new to the go language and I have a semi working model that is outputting to and html template. the next phase i would like to work on is to make it quicker and am curios from a programming aspect is it possible to separate the query into smaller part ? Or does a large query need to be kept as one giant block of code to get scanned into the struct ?

Use orm to do this

now popular orm gorm sqlx

a bit selfish

my toyorm also support association query

https://bigpigeon.org/toyorm

some struct like this

type User struct {
    ID       uint32  `toyorm:"primary key;auto_increment;join:Detail"`
    Name string
    Detail UserDetail
}

type UserDetail struct {
    ID              uint32  `toyorm:"primary key;auto_increment`
    UserID       uint32 `toyorm:"join:Detail"`
    SomeData string 
}

you can accosiation query with preload

brick = toy.Model(&User{}).Preload("Detail").Enter()
data := []User{}
brick.Find(&data)
// select id,name from user
// select id,user_id,some_data from user_detail where user_id in (...)

or with join

brick = toy.Model(&User{}).Join("Detail").Swap()
data := []User{}
brick.Find(&data)
// select m.id,m.name,m_0.id,m_0.user_id,m_0.some_data from user as "m" join user_detail as "m_0" on m.id = m_0.user_id

Thank you for the input and the examples. The syntax look a bit different but i will experiment with this orm.

I think more information such as how your struct and query look like would really help.

That said, I don’t think using an ORM is the answer as you mention you want to make things quicker. Nothing against ORMs, I used ActiveRecord for a couple of years with Rails when I needed ease of development and didn’t have performance requirements, and it worked really well for that. But if you’re seeking speed, ORMs won’t help you. Instead, having full control of your queries is the way to go.

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