Last active
July 6, 2017 07:15
-
-
Save agoalofalife/899760835b132cd5058dc6ad3106b176 to your computer and use it in GitHub Desktop.
Classic exanpl mysql Golang (SELECT)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"database/sql" | |
_ "github.com/go-sql-driver/mysql" | |
"log" | |
"os" | |
"fmt" | |
) | |
type User struct { | |
id int | |
login string | |
password string | |
authId string | |
} | |
func main(){ | |
db, err := sql.Open("mysql", "root:password@/tablename") | |
err = db.Ping() | |
if err != nil { | |
fmt.Println(err.Error()) | |
} | |
results,_ := db.Query("SELECT * FROM `users`") | |
defer results.Close() | |
for results.Next() { | |
var user User | |
err = results.Scan(&user.id, &user.login, &user.password, &user.authId) | |
if err != nil { | |
fmt.Print(err.Error()) | |
} | |
log.Println(user.login) | |
} | |
os.Exit(2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment