GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - xmlgrg/envutils: go env utils

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

43 Commits
43 Commits

Folders and files

NameName
Last commit message
Last commit date
example
example
 
 
.gitignore
.gitignore
 
 
.husky.yaml
.husky.yaml
 
 
.version
.version
 
 
CHANGELOG.md
CHANGELOG.md
 
 
README.md
README.md
 
 
env.go
env.go
 
 
env_test.go
env_test.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
marshal.go
marshal.go
 
 
method_caller.go
method_caller.go
 
 
reflectx.go
reflectx.go
 
 
unmarshal.go
unmarshal.go
 
 

Repository files navigation

envutils

go env utils

Usage

  1. 读取 struct 并将配置文件保存在 config.yml 中
func dump() {

	server := &Server{
		Address: "192.168.100.100",
	}

	config := &struct {
		Server *Server
	}{
		Server: server,
	}

	err := envutils.CallSetDefaults(config)
	if err != nil {
		panic(err)
	}

	b, err := envutils.Marshal(config, appname)
	if err != nil {
		panic(err)
	}
	_ = os.WriteFile(cfgfile, b, os.ModePerm)
}
  1. 查看保存文件
# cat config.yml 
AppName__Server_address: 192.168.100.100
AppName__Server_port: 80
  1. 从文件中读取配置
func read() {

	server := &Server{
		Address: "0.0.0.0",
	}

	config := &struct {
		Server *Server
	}{
		Server: server,
	}

	err := envutils.UnmarshalFile(config, appname, cfgfile)
	if err != nil {
		panic(err)
	}

	fmt.Println("addr=", config.Server.Address)
	fmt.Println("port=", config.Server.Port)
}
// addr= 192.168.100.100
// port= 80

Todo

  • 将结构体的 tag 转换为 config.yml
  • 从环境变量赋值结构体
  • 结构体字段支持 tag 名称

About

go env utils

Resources

Readme
Activity

Stars

1 star

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.