go2struct icon indicating copy to clipboard operation
go2struct copied to clipboard

Convert arbitrary formats to Go Struct (including json, toml, yaml, etc.)

go2struct

Convert arbitrary formats to Go Struct (including json, toml, yaml, etc.)

Installation

Run the following command under your project:

go get -u github.com/NICEXAI/go2struct

Feature

  • [x] Map to struct
  • [x] JSON to struct
  • [x] YAML to struct
  • [ ] TOML to struct

Example

package main

import (
	"encoding/json"
	"fmt"
	"github.com/NICEXAI/go2struct"
)

type Message struct {
	Code    int  `json:"code"`
	Success bool `json:"success"`
	Msg     struct {
		Content string
	} `json:"msg"`
}

func main() {
	var options map[string]interface{}

	message := Message{
		Code: 200,
		Msg:  struct{ Content string }{Content: "Hello"},
	}

	bData, err := json.Marshal(message)
	if err != nil {
		fmt.Printf("json marshal failed, error: %v", err)
		return
	}
	_ = json.Unmarshal(bData, &options)

	//map to struct
	structTxt := go2struct.Map2Struct("message", options)

	fmt.Printf("map to struct success, result: \n%s", structTxt)

	// json to struct
	jsonTemp := `
	{
	 "code": 200,
	 "success": true,
	 "msg": {
		"content": "Hello"
	 }
	}
	`

	res, err := go2struct.JSON2Struct("message", []byte(jsonTemp))
	if err != nil {
		fmt.Printf("json to struct failed, error: \n%v", err)
		return
	}
	fmt.Printf("json to struct success, result: \n%s", res)

	// yaml to struct
	temp := `
code: 200
success: true
msg:
 content: Hello
`

	res, err = go2struct.YAML2Struct("message", []byte(temp))
	if err != nil {
		fmt.Printf("yaml to struct failed, error: \n%v", err)
		return
	}
	fmt.Printf("yaml to struct success, result: \n%s", res)
}