Go錯誤:time.Time未實作driver.Valuer(缺少方法Value)
php小編子墨在使用Go語言開發過程中,可能會遇到一個常見的錯誤:「Go錯誤:time.Time未實作driver.Valuer(缺少方法Value )」。這個錯誤通常在將time.Time類型的值儲存到資料庫中時出現。它表明time.Time類型沒有實作driver.Valuer接口,並且缺少Value方法。解決這個問題的方法有很多,我們可以透過自訂新的時間類型,或是使用第三方函式庫來處理時間類型的儲存。在本文中,我們將詳細介紹這個錯誤的原因和解決方案,幫助開發者更好地理解和解決這個問題。
問題內容
我正在開發一個 Go 項目,在該項目中,我使用 sqlboiler 從我使用 setup.sh 腳本創建的 SQLite3 資料庫生成程式碼。我遇到了一個似乎無法解決的錯誤。錯誤訊息是:
graph/db/repositories.go:998:23: cannot use o.CreatedAt (variable of type string) as driver.Valuer value in argument to queries.MustTime: string does not implement driver.Valuer (missing method Value) graph/db/repositories.go:999:23: cannot use &o.CreatedAt (value of type *string) as sql.Scanner value in argument to queries.SetScanner: *string does not implement sql.Scanner (missing method Scan)
當我嘗試在 graph/db/repositories.go 檔案中使用 time.Time 值時,會發生此錯誤。我正在為我的專案使用 golang:1.21-alpine3.18 Docker 映像。
我嘗試更改 SQLite3 資料庫的儲存庫表中的created_at 列類型。我已經使用 TEXT 和 TIMESTAMP 資料類型對其進行了測試,但錯誤仍然存在。
有誰知道可能導致此錯誤的原因以及如何解決它?
repositories.go
func (o *Repository) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { if o == nil { return errors.New("db: no repositories provided for insertion") } var err error if !boil.TimestampsAreSkipped(ctx) { currTime := time.Now().In(boil.GetLocation()) if queries.MustTime(o.CreatedAt).IsZero() { queries.SetScanner(&o.CreatedAt, currTime) } } if err := o.doBeforeInsertHooks(ctx, exec); err != nil { return err } nzDefaults := queries.NonZeroDefaultSet(repositoryColumnsWithDefault, o) key := makeCacheKey(columns, nzDefaults) repositoryInsertCacheMut.RLock() cache, cached := repositoryInsertCache[key] repositoryInsertCacheMut.RUnlock()
setup.sh
#!/usr/local/bin/sh set -eu readonly DBFILE_NAME="mygraphql.db" # Create DB file if [ ! -e ${DBFILE_NAME} ];then echo ".open ${DBFILE_NAME}" | sqlite3 fi # Create DB Tables echo "creating tables..." sqlite3 ${DBFILE_NAME} " PRAGMA foreign_keys = ON; CREATE TABLE IF NOT EXISTS users(\ id TEXT PRIMARY KEY NOT NULL,\ name TEXT NOT NULL,\ project_v2 TEXT\ ); CREATE TABLE IF NOT EXISTS repositories(\ id TEXT PRIMARY KEY NOT NULL,\ owner TEXT NOT NULL,\ name TEXT NOT NULL,\ created_at TEXT NOT NULL DEFAULT (DATETIME('now','localtime')),\ FOREIGN KEY (owner) REFERENCES users(id)\ ); CREATE TABLE IF NOT EXISTS issues(\ id TEXT PRIMARY KEY NOT NULL,\ url TEXT NOT NULL,\ title TEXT NOT NULL,\ closed INTEGER NOT NULL DEFAULT 0,\ number INTEGER NOT NULL,\ repository TEXT NOT NULL,\ CHECK (closed IN (0, 1)),\ FOREIGN KEY (repository) REFERENCES repositories(id)\ ); CREATE TABLE IF NOT EXISTS projects(\ id TEXT PRIMARY KEY NOT NULL,\ title TEXT NOT NULL,\ url TEXT NOT NULL,\ owner TEXT NOT NULL,\ FOREIGN KEY (owner) REFERENCES users(id)\ ); CREATE TABLE IF NOT EXISTS pullrequests(\ id TEXT PRIMARY KEY NOT NULL,\ base_ref_name TEXT NOT NULL,\ closed INTEGER NOT NULL DEFAULT 0,\ head_ref_name TEXT NOT NULL,\ url TEXT NOT NULL,\ number INTEGER NOT NULL,\ repository TEXT NOT NULL,\ CHECK (closed IN (0, 1)),\ FOREIGN KEY (repository) REFERENCES repositories(id)\ ); CREATE TABLE IF NOT EXISTS projectcards(\ id TEXT PRIMARY KEY NOT NULL,\ project TEXT NOT NULL,\ issue TEXT,\ pullrequest TEXT,\ FOREIGN KEY (project) REFERENCES projects(id),\ FOREIGN KEY (issue) REFERENCES issues(id),\ FOREIGN KEY (pullrequest) REFERENCES pullrequests(id),\ CHECK (issue IS NOT NULL OR pullrequest IS NOT NULL)\ ); " # Insert initial data echo "inserting initial data..." sqlite3 ${DBFILE_NAME} " PRAGMA foreign_keys = ON; INSERT INTO users(id, name) VALUES\ ('U_1', 'hsaki') ; INSERT INTO repositories(id, owner, name) VALUES\ ('REPO_1', 'U_1', 'repo1') ; INSERT INTO issues(id, url, title, closed, number, repository) VALUES\ ('ISSUE_1', 'http://example.com/repo1/issue/1', 'First Issue', 1, 1, 'REPO_1'),\ ('ISSUE_2', 'http://example.com/repo1/issue/2', 'Second Issue', 0, 2, 'REPO_1'),\ ('ISSUE_3', 'http://example.com/repo1/issue/3', 'Third Issue', 0, 3, 'REPO_1')\ ; INSERT INTO projects(id, title, url, owner) VALUES\ ('PJ_1', 'My Project', 'http://example.com/project/1', 'U_1')\ ; INSERT INTO pullrequests(id, base_ref_name, closed, head_ref_name, url, number, repository) VALUES\ ('PR_1', 'main', 1, 'feature/kinou1', 'http://example.com/repo1/pr/1', 1, 'REPO_1'),\ ('PR_2', 'main', 0, 'feature/kinou2', 'http://example.com/repo1/pr/2', 2, 'REPO_1')\ ; "
sqlboiler.toml
pkgname="db" output="graph/db" wipe=true add-global-variants=false no-tests=true [sqlite3] dbname = "./mygraphql.db"
環境
・ MacBook Pro M1 Pro
解決方法
我會說更新 Go 中的儲存庫結構,將 CreatedAt 作為 time.Time 欄位。 將值指派給 CreatedAt 時,請使用 time.Time 值。例如, currTime := time.Now() 讓你的結構像這樣
type Repository struct { ID string `boil:"id" json:"id" toml:"id" yaml:"id"` Owner string `boil:"owner" json:"owner" toml:"owner" yaml:"owner"` Name string `boil:"name" json:"name" toml:"name" yaml:"name"` CreatedAt time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"` // other fields... }
然後設定 CreatedAt 值:
repository := &Repository{ ID: "some-id", Owner: "owner-id", Name: "repo-name", CreatedAt: time.Now(), // setting it as time.Time }
以上是Go錯誤:time.Time未實作driver.Valuer(缺少方法Value)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

Go語言中哪些庫是大公司開發或知名開源項目?在使用Go語言進行編程時,開發者常常會遇到一些常見的需求,�...

Go語言中結構體定義的兩種方式:var與type關鍵字的差異Go語言在定義結構體時,經常會看到兩種不同的寫法:一�...
