Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sample Go code to show the aim
// Code generated by go-bindata.
// sources:
// asset/conf.json
// DO NOT EDIT!
package main
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"strings"
"os"
"time"
"io/ioutil"
"path/filepath"
)
func bindataRead(data []byte, name string) ([]byte, error) {
gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, gz)
clErr := gz.Close()
if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err)
}
if clErr != nil {
return nil, err
}
return buf.Bytes(), nil
}
type asset struct {
bytes []byte
info os.FileInfo
}
type bindataFileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
}
func (fi bindataFileInfo) Name() string {
return fi.name
}
func (fi bindataFileInfo) Size() int64 {
return fi.size
}
func (fi bindataFileInfo) Mode() os.FileMode {
return fi.mode
}
func (fi bindataFileInfo) ModTime() time.Time {
return fi.modTime
}
func (fi bindataFileInfo) IsDir() bool {
return false
}
func (fi bindataFileInfo) Sys() interface{} {
return nil
}
var _assetConfJson = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\x74\x54\x5b\x6f\xda\x4c\x14\x7c\xcf\xaf\xe0\xf3\xd7\xc7\xe2\xe5\xa2\x46\x06\x29\x8a\xaa\x28\x0a\x24\xa4\x08\x12\x68\xca\x0b\xda\xda\x8b\xbd\xb0\xb7\xee\x85\xe2\x54\xfd\xef\xdd\xf5\x3d\xc4\x41\xbc\xf8\xcc\xcc\x99\xf1\xd9\xb3\xfe\x73\xd1\xe9\x78\x1a\x29\x4d\x79\x84\xbc\x71\xc7\xfd\x76\x90\x28\xf4\xd9\x01\x47\x24\x15\xe6\xac\xa8\x77\x3c\x48\x44\x02\xbd\x0c\x12\x5c\xea\xb2\xee\xa0\xe1\x28\xe8\xe7\x08\x4d\x43\xce\x76\x15\xe6\x21\xc6\x78\x81\x40\x66\x20\xb1\x88\x97\x68\x2d\xd4\x18\x80\x88\x87\xca\x8f\x39\x8f\x09\xf2\x43\x4e\x81\x90\x48\x21\xa6\xa1\xb6\xae\x20\x02\xfd\x6e\x34\x18\xde\x2f\xe5\x6c\x4b\x93\x2e\xd9\xbe\x2c\xe7\x3f\xd4\xed\xf3\xea\xdb\x25\x31\xc7\xe9\x52\xdf\xf5\x26\x37\x9b\xc1\xe9\x35\x7d\xb8\x4c\x16\x00\x45\x58\x5f\x1b\x25\xae\x54\x02\x25\x66\x71\xee\xb9\xe3\xd2\x50\x17\xa6\xf2\x8c\x25\x37\xe2\x8d\x6b\xc6\x01\xd7\x09\xb9\xda\xc3\xff\xff\xcb\x9f\x5c\xea\xbd\x00\x79\x13\x8d\x64\xd6\xe3\xe3\xe0\xf6\xd9\x50\x9b\xdc\x85\xde\xcf\xa2\x87\x54\xae\x5f\xcc\x6a\xf1\xfc\x65\x93\xde\x1f\x28\xdd\x04\x13\x7e\x80\x4f\xc7\x6d\x3a\xbd\xbb\x91\xeb\x5f\xa3\xf9\xfa\x75\x91\xf6\x57\x1f\x84\x66\x5c\xe3\x7a\x80\x3b\xa8\x13\x4e\x09\x52\xaa\x2b\xb1\x3d\x92\x6e\x30\x0a\x7a\x7e\x82\x24\x3f\x18\x28\x84\xf3\xaf\x65\x33\x1e\x66\x23\xce\xa3\xda\xa4\xc4\x15\x12\xae\xf4\x38\x08\x86\xa3\xe2\x85\xaa\x62\x2b\xb3\x20\x21\x76\x6c\x9c\xb0\x4b\xd2\x1b\x6c\x23\x6c\x71\xdf\x41\x75\x23\xd4\x5c\x84\x92\xe3\x96\x00\xc7\x79\x53\x04\xf6\xd0\xdf\x2b\xbb\x49\xf9\xee\x40\x6d\x27\xca\x6a\xd5\xb9\xa8\x20\x34\x24\x6e\x45\xdf\xc8\xce\x25\x0d\x42\x43\xa6\x20\x15\xed\xf1\x72\xc4\xd7\xe8\xa4\x4b\x07\x5b\x80\xba\x26\x57\xd4\x0a\x29\x97\x18\xef\x90\xd2\xef\x79\x12\x1d\xbb\x25\xda\xcc\xe0\xd6\x39\x6c\xcd\x90\x21\xc5\xb4\xb1\x7d\x11\x0a\xc3\x96\xa1\x2c\x91\xe2\x46\x86\x48\xb9\xb9\x24\x53\xcb\x7b\xb4\xbc\x4a\xf4\x1b\xb7\x4d\xf2\xbd\xe8\x3b\x66\xcd\x0b\x6a\xbd\xfa\x85\xce\xf3\x7d\x60\xff\x67\xe8\xa0\x44\x3f\x4d\xe6\x8f\xb7\x60\x86\x7f\x4a\x28\x53\xf0\x55\x08\x82\xc3\xec\x86\x76\x9e\x8c\x70\xdf\x81\xec\xb2\x6c\x63\xde\x6c\x50\xa7\xf2\xfc\x66\x9d\x60\x66\x4e\x67\xbe\x17\x7f\xff\x05\x00\x00\xff\xff\xc0\x57\x0b\x95\x87\x04\x00\x00")
func assetConfJsonBytes() ([]byte, error) {
return bindataRead(
_assetConfJson,
"asset/conf.json",
)
}
func assetConfJson() (*asset, error) {
bytes, err := assetConfJsonBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "asset/conf.json", size: 1159, mode: os.FileMode(420), modTime: time.Unix(1454761250, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
// Asset loads and returns the asset for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func Asset(name string) ([]byte, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
}
return a.bytes, nil
}
return nil, fmt.Errorf("Asset %s not found", name)
}
// MustAsset is like Asset but panics when Asset would return an error.
// It simplifies safe initialization of global variables.
func MustAsset(name string) []byte {
a, err := Asset(name)
if (err != nil) {
panic("asset: Asset(" + name + "): " + err.Error())
}
return a
}
// AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func AssetInfo(name string) (os.FileInfo, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
}
return a.info, nil
}
return nil, fmt.Errorf("AssetInfo %s not found", name)
}
// AssetNames returns the names of the assets.
func AssetNames() []string {
names := make([]string, 0, len(_bindata))
for name := range _bindata {
names = append(names, name)
}
return names
}
// _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() (*asset, error){
"asset/conf.json": assetConfJson,
}
// AssetDir returns the file names below a certain
// directory embedded in the file by go-bindata.
// For example if you run go-bindata on data/... and data contains the
// following hierarchy:
// data/
// foo.txt
// img/
// a.png
// b.png
// then AssetDir("data") would return []string{"foo.txt", "img"}
// AssetDir("data/img") would return []string{"a.png", "b.png"}
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
// AssetDir("") will return []string{"data"}.
func AssetDir(name string) ([]string, error) {
node := _bintree
if len(name) != 0 {
cannonicalName := strings.Replace(name, "\\", "/", -1)
pathList := strings.Split(cannonicalName, "/")
for _, p := range pathList {
node = node.Children[p]
if node == nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
}
}
if node.Func != nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
rv := make([]string, 0, len(node.Children))
for childName := range node.Children {
rv = append(rv, childName)
}
return rv, nil
}
type bintree struct {
Func func() (*asset, error)
Children map[string]*bintree
}
var _bintree = &bintree{nil, map[string]*bintree{
"asset": &bintree{nil, map[string]*bintree{
"conf.json": &bintree{assetConfJson, map[string]*bintree{
}},
}},
}}
// RestoreAsset restores an asset under the given directory
func RestoreAsset(dir, name string) error {
data, err := Asset(name)
if err != nil {
return err
}
info, err := AssetInfo(name)
if err != nil {
return err
}
err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
if err != nil {
return err
}
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil {
return err
}
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
if err != nil {
return err
}
return nil
}
// RestoreAssets restores an asset under the given directory recursively
func RestoreAssets(dir, name string) error {
children, err := AssetDir(name)
// File
if err != nil {
return RestoreAsset(dir, name)
}
// Dir
for _, child := range children {
err = RestoreAssets(dir, filepath.Join(name, child))
if err != nil {
return err
}
}
return nil
}
func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
}
package main
import (
"strings"
"github.com/k0kubun/pp"
"github.com/spf13/viper"
)
// C is a global sttruct
type config struct {
Testmode bool
Version string
}
// C is C
var C config
func main() {
c := viper.New()
asset, _ := Asset("asset/conf.json")
c.ReadConfig(strings.NewReader(string(asset)))
c.Unmarshal(&C)
pp.Println(C)
}
@hachi8833
Copy link
Author

hachi8833 commented Feb 6, 2016

Running the code above shows that the unmarshalled structure C is empty:

$go run bindata.go test.go
main.config{
Testmode: false,
Version: "",
}

@pnguyen-okta
Copy link

pnguyen-okta commented Jun 13, 2020

I know this is 4 years old, but here is the solution to the problem:

on line 21, add:

c.SetConfigType("json")

@hachi8833
Copy link
Author

hachi8833 commented Jun 13, 2020

thanks!👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment