Created
November 20, 2016 14:05
-
-
Save EtienneR/df0df0490758ea10d8f1c25b59885f7e to your computer and use it in GitHub Desktop.
Golang YAML to JSON with Gin : https://medium.com/@etiennerouzeaud/golang-yaml-to-json-with-gin-e2e1529274b1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'title': 'This is my go blog' | |
'email': '[email protected]' | |
'tags': [go, golang, yaml, json] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"io/ioutil" | |
"gopkg.in/gin-gonic/gin.v1" | |
"gopkg.in/yaml.v2" | |
) | |
type Config struct { | |
Title string `json:"title"` | |
Email string `json:"email"` | |
Tags []string `json:"tags"` | |
} | |
func main() { | |
r := gin.Default() | |
r.GET("", func(c *gin.Context) { | |
data, err := ioutil.ReadFile("config.yml") | |
if err != nil { | |
log.Fatalf("error: %v", err) | |
} | |
var config Config | |
err = yaml.Unmarshal([]byte(data), &config) | |
if err != nil { | |
log.Fatalf("error: %v", err) | |
} | |
result := Config{ | |
Title: config.Title, | |
Email: config.Email, | |
Tags: config.Tags, | |
} | |
c.JSON(200, result) | |
}) | |
r.Run(":3000") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment