package main
import (
"database/sql"
"fmt"
"time"
)
type Student struct {
Id int64
Name string
Age int
Birthday string
}
func ConnectDB() (db *sql.DB, err error) {
dbDriver := "mysql"
dbUser := "root"
dbPass := "nilpointer.net"
dbName := "nilpointer_db"
db, err = sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
return
}
func main() {
db, err := ConnectDB()
if err != nil {
fmt.Println(err)
} else {
startDate, _ := time.Parse("2006-01-02", "2022-11-14")
endDate, _ := time.Parse("2006-01-02", "2022-12-30")
rows, err2 := db.Query("select * from student where Birthday >= ? and Birthday <= ?", startDate.Format("2006-01-02"), endDate.Format("2006-01-02"))
if err2 != nil {
fmt.Println(err2)
} else {
students := []Student{}
for rows.Next() {
var student Student
err3 := rows.Scan(&student.Id, &student.Name, &student.Age, &student.Birthday)
if err3 != nil {
fmt.Println(err3)
} else {
students = append(students, student)
}
}
fmt.Println(students)
}
}
}