I am using yaml to decode yaml file. However, the result is not as expected. The EncryptKey
is not extracted. This is my test code:
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
var data = `
port: 8080
encryptKey: "jfgjfgkfgd"
`
type Config struct {
Port int `json:"port"`
EncryptKey string `json:"encryptKey"`
}
func main() {
t := Config{}
err := yaml.Unmarshal([]byte(data), &t)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- t:
%v
", t)
}
I got the result like this:--- t: {8080 }
It's my carelessness. I should use the tag yaml
instead of json
.
You are using json
tags instead of yaml
tags. Fix your struct definition like this:
type Config struct {
Port int `yaml:"port"`
EncryptKey string `yaml:"encryptKey"`
}