I want to learn to write clean code in golang, my problem is: I have two function and I need to combined to become only one, this is my actual code:
func db_execute(sql_cmd string) bool {
db, err := sql.Open("mysql", mysql_login)
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
log.Fatal(err)
}
stmt, err := db.Prepare(sql_cmd)
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec()
if err != nil {
fmt.Print(err.Error())
return false
}
return true
}
func db_tabela_select(cod_cliente string) (id string, cod1 string, cod2 string, cod3 string) {
db, err := sql.Open("mysql", mysql_login)
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
log.Fatal(err)
}
err = db.QueryRow("select id, cod1, cod2, cod3 from db_tabela WHERE `cliente`=? LIMIT 1;", cod_cliente).Scan(
&id, &cod1, &cod2, &cod3)
switch {
case err == sql.ErrNoRows:
log.Printf("No codice cliente with that ID.")
case err != nil:
log.Printf("database select problems")
default:
fmt.Printf("Client code is %s %s %s
", cod1, cod2, cod3)
}
return id, cod1, cod2, cod3
}
How can I combine these 2 function in one? These functions have different input and different output.