我的 go 1.21.1 配置一定不完整/或设置错误,因为我无法导入/使用模块。
我想要这些设置
c:\dev\golang\
下,例如 c:\dev\golang\gormHelloWorld
c:\dev\godeps\
下,那就太好了,但不是必需的目前我有这些设置
set GOPATH=c:\dev\golang
和 set GOROOT=C:\Program Files\Go
%http_proxy%
和 %https_proxy%
均设置为 http://<myuser>:<mypass>@our.proxy.abc:8080
(无 https 仅 http)GOROOT
,因为C:\Program Files\Go>dir /s *gorm*
返回未找到文件。我有这些假设:
GOWORK
或GOROOT
我已读过
C:\dev\golang>mkdir gormHelloWorld
并 cd 到 gormHelloWorld
C:\dev\golang\gormHelloWorld>
在
ECHO package main>hellogorm.go
C:\dev\golang\gormHelloWorld>code .
hellogorm.go
go get -u gorm.io/gorm
和 go get -u gorm.io/driver/sqlite
都不会返回任何内容 <-- could this be a proxy issue?C:\dev\golang\gormHelloWorld>go run .
,结果是
hellogorm.go:4:6: missing import path
could not import gorm.io/driver/sqlite (cannot find package "gorm.io/driver/sqlite" in GOROOT or GOPATH)
C:\dev\golang\gormHelloWorld>go env
请参阅下面的列表。文件
hellogorm.go
有代码
package main
import (
"fmt"
"gorm.io/gorm" // could not import . cannot find package
// in GOROOT or GOPATH
"gorm.io/driver/sqlite" // could not ... as above
)
type Product struct {
gorm.Model
Code string
Price uint
}
func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&Product{})
var product Product
if db.First(&product).Error == gorm.ErrRecordNotFound {
fmt.Println("no product record, creating now...")
db.Create(&Product{Code: "D42", Price: 100})
} else {
fmt.Printf("product record found: %v", product)
}
}
go env
的输出。所有空条目(如 set GOBIN=
)均已删除。
C:\dev\golang>go env
set GO111MODULE=off
set GOARCH=amd64
set GOCACHE=C:\Users\<myuser>\AppData\Local\go-build
set GOENV=C:\Users\<myuser>\AppData\Roaming\go\env
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOMODCACHE=C:\Users\<myuser>\go\pkg\mod
set GOOS=windows
set GOPATH=C:\Users\<myuser>\go
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVERSION=go1.21.1
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOWORK=C:\dev\golang\<SomeOtherProject>\gorm\go.work
set CGO_CFLAGS=-O2 -g
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics
-Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0
-ffile-prefix-map=%LocalAppData%\Temp\go-build4080445693=/tmp/go-build
-gno-record-gcc-switches
添加了以上换行符,并将
C:\Users\<myuser>\AppData\Local\
替换为 %LocalAppData%
以避免出现滚动条。
我的
%gopath%
指向 c:\dev\golang 并且我的文件夹结构是错误的。
从 jetbrains.com 阅读 GOROOT 和 GOPATH 后,我将文件夹结构更改为这样
C:\>echo %gopath%
C:\dev\golang
C:\cygwin64\bin>tree -L 2 %gopath%
C:\dev\golang
├── bin
│ └── gopls.exe
├── pkg
│ ├── mod
│ ├── sumdb
│ └── windows_amd64
└── src
├── HelloMods
├── gormHelloWorld
└── goHello
我将所有项目文件夹移到了里面
C:\dev\golang\src\
。
示例
C:\dev\golang\gormHelloWorld\
<-- did not workC:\dev\golang\src\gormHelloWorld\
<-- does work之后我修复时遇到了一些麻烦
failed to initialize database, got error Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work
在我将winlabs中的gcc.exe添加到我的路径中之后,gorm确实构建并创建了test.db