Go语言从入门到规范-6.9、Go处理yml和ini文件

2 篇文章 0 订阅

Go语言从入门到规范-6.9、Go处理yml和ini文件


1. 前言

一般yml和ini都是作为配置文件来使用的,但是实际上它们都是一种新的语言,主要是方便序列化数据便于我们进行互联网数据传输而创建的,虽然它们没有json和xml那么频繁的使用,但是也是有很多使用场景的,之前我们已经对yml做了简单介绍:https://blog.csdn.net/weixin_39510813/article/details/115633101

接下来我们再对ini做学习和总结。

2. ini概念

2.1 概述

INI文件是一种无固定标准格式的配置文件。它以简单的文字与简单的结构组成,常常使用在Windows操作系统上,许多程序也会采用INI文件做为配置文件之用。Windows操作系统后来以注册表的形式取代掉INI档。INI文件的命名来源,是取自英文“初始(Initial)”的首字缩写,正与它的用途——初始化程序相应。有时候,INI文件也会以不同的扩展名,如“.CFG”、“.CONF”、或是“.TXT”代替。

目前还有很多程序和软件使用ini文件进行初始化以及作为一些配置文件来使用。

2.2 格式

节:section

参数:键=值 name=value

注解:注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。

2.3 示例

; last modified 1 April 2001 by John Doe
[owner]
name=John Doe
organization=Acme Products

[database]
server=192.0.2.42 ; use IP address in case network name resolution is not working
port=143
file="acme payroll.dat"

比如mysql数据我们就可以借助ini文件对其进行一些配置。

3. Go语言处理ini文件

gopkg.in/ini.v1

使用说明:

https://ini.unknwon.io/docs/intro/getting_started

创建一个my.ini文件:

# possible values : production, development
app_mode = development

[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
data = /home/git/grafana

[server]
# Protocol (http or https)
protocol = http

# The http port  to use
http_port = 9999

# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
enforce_domain = true

很好,接下来我们需要编写 main.go 文件来操作刚才创建的配置文件。

package main

import (
    "fmt"
    "os"

    "gopkg.in/ini.v1"
)

func main() {
    cfg, err := ini.Load("my.ini")
    if err != nil {
        fmt.Printf("Fail to read file: %v", err)
        os.Exit(1)
    }

    // 典型读取操作,默认分区可以使用空字符串表示
    fmt.Println("App Mode:", cfg.Section("").Key("app_mode").String())
    fmt.Println("Data Path:", cfg.Section("paths").Key("data").String())

    // 我们可以做一些候选值限制的操作
    fmt.Println("Server Protocol:",
        cfg.Section("server").Key("protocol").In("http", []string{"http", "https"}))
    // 如果读取的值不在候选列表内,则会回退使用提供的默认值
    fmt.Println("Email Protocol:",
        cfg.Section("server").Key("protocol").In("smtp", []string{"imap", "smtp"}))

    // 试一试自动类型转换
    fmt.Printf("Port Number: (%[1]T) %[1]d\n", cfg.Section("server").Key("http_port").MustInt(9999))
    fmt.Printf("Enforce Domain: (%[1]T) %[1]v\n", cfg.Section("server").Key("enforce_domain").MustBool(false))
    
    // 差不多了,修改某个值然后进行保存
    cfg.Section("").Key("app_mode").SetValue("production")
    cfg.SaveTo("my.ini.local")
}

结果:

App Mode: development
Data Path: /home/git/grafana
Server Protocol: http
Email Protocol: smtp
Port Number: (int) 9999
Enforce Domain: (bool) true
要在GitLab CI/CD中使用Go语言项目并上传`.gitlab-ci.yml`文件,你需要创建一个`.gitlab-ci.yml`文件来描述自动化流程,同时编写一个Dockerfile来定义CI/CD使用的镜像。这里是一个简单的例子: **.gitlab-ci.yml (ci.yml)**: ```yaml image: docker:stable # 使用官方Docker最新稳定版 services: - docker:dind # 配置Docker in Docker服务以构建Docker镜像 stages: - deploy before_script: - apt-get update -yqq - apt-get install -yqq curl gnupg lsb-release install_golang: stage: build script: - curl -fsSL https://get.golang.org | sh -s stable - export PATH=$PATH:$GOPATH/bin build: stage: build script: -o main your-go-file.go artifacts: paths: - main deploy: stage: deploy script: - echo "Uploading the binary..." - docker login -u $GITLAB_CI_REGISTRY_USER -p $GITLAB_CI_REGISTRY_PASSWORD $CI_REGISTRY - docker push $CI_REGISTRY_IMAGE ``` 在这个YAML文件里,我们设置了三个阶段(build, test, deploy),首先安装Go语言,然后构建Go应用,最后将二进制文件推送到GitLab仓库的注册表。 **Dockerfile**: ```dockerfile # 使用官方的 Go 语言镜像作为基础 FROM golang:latest # 工作目录 WORKDIR /app # 将当前目录内容复制到容器内 COPY . . # 安装所需的依赖 RUN go get -v -t -d ./... # 设置运行应用的命令 CMD ["/bin/bash", "-c", "./your-go-file.go"] ``` 这个Dockerfile用于创建一个包含Go项目的镜像,然后在其中运行你的Go应用。 记得替换`your-go-file.go`为你实际的Go源文件名。并将`$GITLAB_CI_REGISTRY_USER`和`$GITLAB_CI_REGISTRY_PASSWORD`替换为你的GitLab CI/CD服务的用户名和密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昵称系统有问题

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值