8000 GitHub - yushuai1/gonvert: Provide the transformation of map and struct for the go language
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

yushuai1/gonvert

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

刚学Go,练练手.

gonvert

Provide the transformation of map and struct for the go language

安装

go get -u -t github.com/gotail/gonvert

Struct 转换为 Map

package main

import (
	"fmt"

	"github.com/gotail/gonvert"
)

type Person struct {
	Username string `gonvert:"username"`
	Age      int    `gonvert:"age"`
	Company  string `gonvert:"company"`
}

func main() {
	person := Person{Username: "golang", Age: 10, Company: "Github"}
	PersonMap := gonvert.Struct2Map(&person) // 该方法可传入结构体或结构体指针
	fmt.Println(PersonMap)
}

Map转换为 Struct

package main

import (
	"fmt"

	"github.com/gotail/gonvert"
)

type Person struct {
	Username string `gonvert:"username"`
	Age      int `gonvert:"age"`
	Company  string `gonvert:"company"`
	Jh bool	`gonvert:"jh"`
}

var data = map[string]interface{}{
	"username": "golang",
	"age":      30,
	"company":  "Github",
	"jh":true,
}

func main() {
	person := &Person{}
	gonvert.Map2Struct(data, person)
	fmt.Println(person)
}

About

Provide the transformation of map and struct for the go language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%
0