Are there any good ways to make this createIndexBlock()
method shorter?
It looks wastefully long. I have many tables. I don't like to make these kinds of methods.
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
var db, _ = sql.Open("mysql", "xxx:xxx@(database:3306)/xx")
func createIndexBlock(blockHash string) int64 {
rows, err := db.Query("SELECT id FROM index_blocks WHERE `hash`=? LIMIT 1", blockHash)
if err != nil {
panic(err.Error())
}
for rows.Next() {
var id int64
err := rows.Scan(&id)
if err != nil {
panic(err.Error())
}
rows.Close()
return id
}
result, err := db.Exec("INSERT INTO index_blocks (`hash`) values (?)", blockHash)
if err != nil {
panic(err.Error())
}
id, err := result.LastInsertId()
if err != nil {
panic(err.Error())
}
return id
}
index_blocks
table look like, and what is it for? \$\endgroup\$