Apologies for try-ing

rocketlaunchr.cloud
Jul 24 · 4 min read
type ColorGroup struct {
ID int
Name string
Colors []string
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
return
}
os.Stdout.Write(b)

Try Proposal

Official try proposal document

igo — An alternative Go transpiler

dbq — Barbeque the boiler plate code

Examples

| id | name  | age | created_at |
|----|-------|-----|------------|
| 1 | Sally | 12 | 2019-03-01 |
| 2 | Peter | 15 | 2019-02-01 |
| 3 | Tom | 18 | 2019-01-01 |

Bulk Insert

db, _ := sql.Open("mysql", "user:password@tcp(localhost:3306)/db")users := []interface{}{
[]interface{}{"Brad", 45, time.Now()},
[]interface{}{"Ange", 36, time.Now()},
[]interface{}{"Emily", 22, time.Now()},
}
stmt := dbq.INSERT("users", []string{"name", "age", "created_at"}, len(users))dbq.E(ctx, db, stmt, nil, users)

Query

type user struct {
ID int `dbq:"id"`
Name string `dbq:"name"`
Age int `dbq:"age"`
CreatedAt time.Time `dbq:"created_at"`
}

opts := &dbq.Options{ConcreteStruct: user{}, DecoderConfig:x}

results, err := dbq.Q(ctx, db, "SELECT * FROM users", opts)

Query Single Row

result := dbq.MustQ(ctx, db, "SELECT * FROM users LIMIT 1", dbq.SingleResult)
if result == nil {
// no result
} else {
result.(map[string]interface{})
}

MySQL cancelation

import (
stdSql "database/sql"
sql "github.com/rocketlaunchr/mysql-go"
)
p, _ := stdSql.Open("mysql", "user:password@tcp(localhost:3306)/db")pool := &sql.DB{DB:p}conn, err := pool.Conn(ctx)
defer conn.Close()
result := dbq.MustQ(ctx, conn, "SELECT * FROM users LIMIT 1", dbq.SingleResult)
if result == nil {
// no result
} else {
result.(map[string]interface{})
}

Final Notes

Links

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade