zl程序教程

您现在的位置是:首页 >  其他

当前栏目

MacOS 安装 go-sqlite3 问题与解决

2023-04-18 14:43:36 时间

如果直接下载的话,报错如下:

go get github.com/mattn/go-sqlite3
go get: module github.com/mattn/go-sqlite3: reading https://athens.azurefd.net/github.com/mattn/go-sqlite3/@v/list: 504 Gateway Timeout

第一步

Mac OS X
1. 经过 Homebrewn 安装: html

brew install pkgconfig
brew install sqlite3

执行结果如下:

➜  ~ brew install pkgconfig
Warning: pkg-config 0.29.2_3 is already installed and up-to-date.
To reinstall 0.29.2_3, run:
  brew reinstall pkg-config
➜  ~ brew install sqlite3
Warning: sqlite 3.37.0 is already installed and up-to-date.
To reinstall 3.37.0, run:
  brew reinstall sqlite

第二步

brew link pkgconfig --force
brew link sqlite3 --force

第三步

go get github.com/mattn/go-sqlite3

结果如下:

go: downloading github.com/mattn/go-sqlite3 v1.14.9

sqllite 测试用例

package mysql

import (
 "fmt"
 "testing"

 "github.com/jinzhu/gorm"
 //_ "github.com/jinzhu/gorm/dialects/sqlite"
 _ "github.com/mattn/go-sqlite3"
)

type Product struct {
 gorm.Model
 Code  string
 Price uint
}

func (Product) TableName() string {
 return "hax_products"
}
func Test(t *testing.T) {
 db, err := gorm.Open("sqlite3", "test.db")
 if err != nil {
  panic("failed to connect database")
 }
 defer db.Close()
 gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
  return "hax_" + defaultTableName
 }
 db.LogMode(true)
 // Migrate the schema
 db.AutoMigrate(&Product{})
 db.Create(&Product{Code: "L1212", Price: 1000})
 var product Product
 db.First(&product, 1)
 var products []Product
 db.Find(&products)
 fmt.Printf("Total count %d", len(products))
}

macos 安装有问题,可以参考 https://segmentfault.com/q/1010000000162180 这个解决

参考资料

  • https://www.shangmayuan.com/a/3b17868c37bb4475a8b8c0f4.html
  • https://www.cnblogs.com/go-ios/p/3871863.html
  • go get github.com/mattn/go-sqlite3
  • https://segmentfault.com/q/1010000000162180