当前位置: 首页 > news >正文

go打包配置文件

序言

一开始用的go-bindata,但是到了Linux下发现,怎么用配置都不生效,然后看到了embed方法

使用

package config

import (
	"bytes"
	_ "embed"
	"log"

	"github.com/spf13/viper"
)

var Config struct {
	// 这里定义你的配置字段,例如:
	Port int
	Env  string
}

//go:embed config.yaml
var configData []byte

func init() {
	// 从 embed 的变量中读取配置
	viper.SetConfigType("yaml") // 明确指定配置类型
	if err := viper.ReadConfig(bytes.NewBuffer(configData)); err != nil {
		log.Fatalf("Error reading config file: %v", err)
	}

	if err := viper.Unmarshal(&Config); err != nil {
		log.Fatalf("Unable to decode into struct: %v", err)
	}
}

your_project/
├── config/
│   ├── config.yaml
│   └── config.go   <-- 放置上面这个代码
└── main.go

这个是configs文件夹下的config.go
这个下面有config.yaml
需要注意的是
var configData []byte 上面的注释不是普通注释,他是告诉打包编译那个yaml文件


文章转载自:
http://calciphobous.zzyjnl.cn
http://adlittoral.zzyjnl.cn
http://aniconic.zzyjnl.cn
http://chainreactor.zzyjnl.cn
http://chiricahua.zzyjnl.cn
http://ariadne.zzyjnl.cn
http://buteshire.zzyjnl.cn
http://burnsides.zzyjnl.cn
http://breakout.zzyjnl.cn
http://affrontive.zzyjnl.cn
http://camauro.zzyjnl.cn
http://chemical.zzyjnl.cn
http://caliduct.zzyjnl.cn
http://bade.zzyjnl.cn
http://chacma.zzyjnl.cn
http://carrefour.zzyjnl.cn
http://buskin.zzyjnl.cn
http://asce.zzyjnl.cn
http://chemoreceptive.zzyjnl.cn
http://bemuse.zzyjnl.cn
http://areopagus.zzyjnl.cn
http://bemaze.zzyjnl.cn
http://airplane.zzyjnl.cn
http://beslaver.zzyjnl.cn
http://agog.zzyjnl.cn
http://bicornuous.zzyjnl.cn
http://cartelization.zzyjnl.cn
http://cholecystokinetic.zzyjnl.cn
http://agamont.zzyjnl.cn
http://astonied.zzyjnl.cn
http://www.dtcms.com/a/123923.html

相关文章:

  • 【自然语言处理】深度学习中文本分类实现
  • 鸿蒙5.0 非桌面页面,设备来电后挂断,自动返回桌面
  • 计算机网络起源
  • lanqiaoOJ 2489 进制
  • LangChain-记忆系统 (Memory)
  • 【JAVA】JVM 堆内存“缓冲空间”的压缩机制及调整方法
  • 前端-Vue2组件化编程
  • 电脑卡顿严重怎么办 电脑卡顿的处理指南
  • WPF 五子棋项目文档
  • 电脑命名配置很高,为什么运行软件特别卡
  • transformers:打造的先进的自然语言处理
  • jdk21、17、11安装包
  • 【前端】事件循环专题
  • 系统与网络安全------网络通信原理(6)
  • 【Steam mod 下载方法】
  • 机器视觉+深度学习,让电子零部件表面缺陷检测效率大幅提升
  • 【windows10】基于SSH反向隧道公网ip端口实现远程桌面
  • P8667 [蓝桥杯 2018 省 B] 递增三元组
  • 禁止页面滚动的方法-微信小程序
  • Hive常见故障多案例FAQ宝典 --项目总结(宝典一)
  • 文件上传做题记录
  • Linux自行实现的一个Shell(15)
  • 《Operating System Concepts》阅读笔记:p601-p605
  • 庙算兵推:使用Streamlit框架构建了一个智能作战推演系统。
  • 图灵逆向——题十七-字体加密
  • 常见的 set 选项与空变量检查
  • QML Item 元素
  • stack容器(栈容器)
  • RT-Thread 屏蔽在线软件包的方法
  • 【愚公系列】《高效使用DeepSeek》064-跨文化谈判