代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/gorm"
  5. _ "github.com/jinzhu/gorm/dialects/mysql"
  6. )
  7. type Test struct {
  8. //gorm.Model
  9. Id int `gorm:"primary_key;auto_increment"`
  10. Telephone string
  11. First string
  12. Last string
  13. }
  14. var db *gorm.DB
  15. func main() {
  16. var err error
  17. connArgs := fmt.Sprintf(
  18. "%s:%s@(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
  19. "root", "root", "localhost", 3306, "test")
  20. db, err := gorm.Open("mysql", connArgs)
  21. //db, err := gorm.Open("mysql", "root:root@tcp(localhost:3306)/test")
  22. if err != nil {
  23. panic(err)
  24. }
  25. defer db.Close()
  26. db.LogMode(true)
  27. fmt.Println()
  28. var per = Test{Id: 2, First: "LejuRobot", Last: "test", Telephone: "23434"}
  29. err = db.Create(&per).Error
  30. if err != nil {
  31. fmt.Println(err)
  32. }
  33. db.Save(&per)
  34. }

参考:
这是官网

分类: web

标签:   golang