go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

how to deal $ in config file

Open lvphpwb opened this issue 6 months ago • 5 comments
trafficstars

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior, if applicable:

  1. The code is

    Price: '$1.0'
    
  2. The error is

    Price   .0
    

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Environments (please complete the following information):

  • OS: [e.g. Linux]
  • go-zero version [e.g. 1.2.1]
  • goctl version [e.g. 1.2.1, optional]

More description Add any other context about the problem here.

lvphpwb avatar May 19 '25 10:05 lvphpwb

Use the latest version. It works for "$1.0" in config.

kevwan avatar May 19 '25 13:05 kevwan

使用1.8.3版本还是有问题

lvphpwb avatar May 20 '25 04:05 lvphpwb

main.go

package main

import (
	"flag"
	"fmt"

	"github.com/zeromicro/go-zero/core/conf"
)

type Config struct {
	Price string
}

var configFile = flag.String("f", "config.yaml", "the config file")

func main() {
	flag.Parse()

	var c Config
	conf.MustLoad(*configFile, &c)
	fmt.Println(c.Price)
}

config.yaml

Price: '$1'

No problem here. You can check it.

kevwan avatar May 20 '25 11:05 kevwan

conf.MustLoad(*configFile, &c, conf.UseEnv())

lvphpwb avatar May 21 '25 01:05 lvphpwb

os.ExpandEnv() doesn't support escaping $ char. So if you want to use $ char, don't use conf.UseEnv().

kevwan avatar May 24 '25 08:05 kevwan