即使在设置结构标签后也无法解析 TOML 文件
Unable to parse TOML file even after setting struct tags
我安装依赖使用:
go get github.com/BurntSushi/toml
我在 main.go
:
所在的文件夹中创建了一个 toml 文件
.
|-- cloud.toml
`-- main.go
cloud.toml
[database]
host = "localhost"
port = 8086
secure = false
username = "test"
password = "password"
dbName = "test"
main.go
package main
import (
"fmt"
"github.com/BurntSushi/toml"
)
type tomlConfig struct {
DB dbInfo
}
type dbInfo struct {
Host string `toml:"host"`
Port int `toml: "port"`
Secure bool `toml: "secure"`
Username string `toml: "username"`
Password string `toml: "password"`
DbName string `toml:"dbName"`
}
func main() {
var dbConfig tomlConfig
if _, err := toml.DecodeFile("cloud.toml", &dbConfig); err != nil {
fmt.Println(err)
return
}
fmt.Println("Database Configuration")
fmt.Printf("Host: %s\n", dbConfig.DB.Host)
fmt.Printf("Port: %d\n", dbConfig.DB.Port)
}
输出
go run main.go
Database Configuration
Host:
Port: 0
我做错了什么?
我的 go env
是:
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\des\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\des\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\des\AppData\Local\Temp\go-build309995570=/tmp/go-build -gno-record-gcc-switches
您需要将适当的 toml 标签添加到您的结构中:
type tomlConfig struct {
DB dbInfo `toml:"database"`
}
您还应该删除其他标签中的空格,以使其有效:
type dbInfo struct {
Host string `toml:"host"`
Port int `toml:"port"`
Secure bool `toml:"secure"`
Username string `toml:"username"`
Password string `toml:"password"`
DbName string `toml:"dbName"`
}
我安装依赖使用:
go get github.com/BurntSushi/toml
我在 main.go
:
.
|-- cloud.toml
`-- main.go
cloud.toml
[database]
host = "localhost"
port = 8086
secure = false
username = "test"
password = "password"
dbName = "test"
main.go
package main
import (
"fmt"
"github.com/BurntSushi/toml"
)
type tomlConfig struct {
DB dbInfo
}
type dbInfo struct {
Host string `toml:"host"`
Port int `toml: "port"`
Secure bool `toml: "secure"`
Username string `toml: "username"`
Password string `toml: "password"`
DbName string `toml:"dbName"`
}
func main() {
var dbConfig tomlConfig
if _, err := toml.DecodeFile("cloud.toml", &dbConfig); err != nil {
fmt.Println(err)
return
}
fmt.Println("Database Configuration")
fmt.Printf("Host: %s\n", dbConfig.DB.Host)
fmt.Printf("Port: %d\n", dbConfig.DB.Port)
}
输出
go run main.go
Database Configuration
Host:
Port: 0
我做错了什么?
我的 go env
是:
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\des\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\des\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\des\AppData\Local\Temp\go-build309995570=/tmp/go-build -gno-record-gcc-switches
您需要将适当的 toml 标签添加到您的结构中:
type tomlConfig struct {
DB dbInfo `toml:"database"`
}
您还应该删除其他标签中的空格,以使其有效:
type dbInfo struct {
Host string `toml:"host"`
Port int `toml:"port"`
Secure bool `toml:"secure"`
Username string `toml:"username"`
Password string `toml:"password"`
DbName string `toml:"dbName"`
}