go语言操作mysql数据库

golang操作mysql数据库

go get github.com/go-sql-driver/mysql

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

type Class struct {
	id int
	name string
}
go

func main() {
	db, _ := sql.Open("mysql", "root:12345@/test1")
	defer db.Close()
	//result,err:=db.Exec("insert into student_class ( class_name) VALUES (?)","class1")
	//fmt.Println(result)
	//fmt.Println(err)
	rows,err:=db.Query("select * from student_class")
	fmt.Println(rows)
	fmt.Println(err)
	defer rows.Close()
	class1:=&Class{}
	for rows.Next() {
		rows.Scan(&class1.id,&class1.name)
	}
	fmt.Println(class1)

}

上一篇:记录一次泛型的使用


下一篇:VUE 动态绑定class类的三种常用方法及适用场景