非项目目录下命令行执行go程序遇到的文件路径问题

非项目目录下命令行执行go程序遇到的问题

问题发现

当我们运行我们项目的时候,我们通过go build 把我们的项目编译成了一个二进制的可执行文件,在当前目录下执行这个可执行文件是没有问题的

image-20211125113705890

image-20211125144754234

但是如果我们切去其他的目录的话,通过路径去执行这个可执行文件,就会报错!因为我们启动项目要读取一个关于项目环境的配置文件,在[/Users/codehope/setup]这里找不到这个配置文件,因为他编译后,在程序找配置文件,是相对于当前执行目录去寻找的配置文件,而不是基于项目目录结构(可能此时不存在什么目录结构了)

image-20211125144949774

如何解决-通过命令行参数注入

方式1 利用os.Args

前情提要,我们的配置文件是通过viper读取的,所以通过命令行的方式去注入配置文件的路径,这样就不是基于项目去寻找了

首先我们先在viper初始化的时候打印一下os.Args

/*
    InitAppConfig 初始化vipper的配置文件
*/
func InitAppConfig() (error error) {
    fmt.Println(os.Args)
....

build代码后

~/remote-es-server-code/go-cli-v1/go-cli-v1

切去非当前项目目录下去执行(依然是报错的,但是可以看到os.Args的打印结果=>[/Users/codehope/remote-es-server-code/go-cli-v1/go-cli-v1])

那么我们就可以通过传入第二个参数去注入配置文件的路径了

~/remote-es-server-code/go-cli-v1/go-cli-v1 ~/remote-es-server-code/go-cli-v1/config

image-20211125145450595

发现这种就可以成功的跑起来啦!

方法2 标准库flag

标准库flag文档

//flag.Type(flag名, 默认值, 帮助信息)*Type  (定义某种数据类型返回对应指针类型)
configPath := flag.String("config", "", "项目配置文件") 

定义了flag可以通过运行项目 -h来查看帮助信息

image-20211125150647640

configPath := flag.String("config", "", "项目配置文件")
flag.Parse()
fmt.Println(*configPath)

执行:可以通过这种方式注入配置

image-20211125150829257

viper.AddConfigPath(*configPath) //设置配置文件path

完整版

$~/remote-es-server-code/go-cli-v1/go-cli-v1 -config /Users/codehope/remote-es-server-code/go-cli-v1/config          
  /Users/codehope/remote-es-server-code/go-cli-v1/config
  [/Users/codehope/remote-es-server-code/go-cli-v1/go-cli-v1 -config /Users/codehope/remote-es-server-code/go-cli-v1/config]
  数据库初始化成功!
  redis初始化成功!
  [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
   - using env:   export GIN_MODE=release
   - using code:  gin.SetMode(gin.ReleaseMode)

  [GIN-debug] POST   /public/api/v1/user/register --> go-cli-v1/controller.(*UserController).RegisterNewUser-fm (4 handlers)
  [GIN-debug] POST   /public/api/v1/user/login --> go-cli-v1/controller.(*UserController).UserLogin-fm (4 handlers)
  [GIN-debug] GET    /private/api/v1/user/profile --> go-cli-v1/controller.(*UserController).GetUserProfile-fm (5 handlers)
  [GIN-debug] POST   /private/api/v1/category  --> go-cli-v1/controller.(*CategoryController).UserCreateNewCategory-fm (5 handlers)
  [GIN-debug] GET    /private/api/v1/category  --> go-cli-v1/controller.(*CategoryController).UserQueryAllCategoryList-fm (5 handlers)
  [GIN-debug] GET    /public/api/v1/category/:category_id --> go-cli-v1/controller.(*CategoryController).QueryCategoryDetailByCategoryId-fm (4 handlers)
  [GIN-debug] POST   /private/api/v1/post      --> go-cli-v1/controller.(*PostController).UserCreateNewPost-fm (5 handlers)
  [GIN-debug] PUT    /private/api/v1/post      --> go-cli-v1/controller.(*PostController).UserUpDatePost-fm (5 handlers)
  [GIN-debug] DELETE /private/api/v1/post      --> go-cli-v1/controller.(*PostController).UserDeletePost-fm (5 handlers)
  [GIN-debug] GET    /public/api/v1/post/:post_id --> go-cli-v1/controller.(*PostController).PostDetail-fm (4 handlers)
  [GIN-debug] GET    /private/api/v1/post      --> go-cli-v1/controller.(*PostController).UserPostList-fm (5 handlers)
  [GIN-debug] GET    /public/api/v1/post       --> go-cli-v1/controller.(*PostController).PublicPostList-fm (4 handlers)
  [GIN-debug] POST   /private/api/v1/comments  --> go-cli-v1/controller.(*CommentsController).CreateComments-fm (5 handlers)
  [GIN-debug] GET    /public/api/v1/comments/:post_id --> go-cli-v1/controller.(*CommentsController).QueryPostComment-fm (4 handlers)
  [GIN-debug] GET    /public/api/v1/comments2comments/:comment_id --> go-cli-v1/controller.(*CommentsController).QueryComments2Comment-fm (4 handlers)
  [GIN-debug] GET    /transitionTest           --> go-cli-v1/routes.Init.func1 (4 handlers)
  [GO ! GO ! GO ! ]  Server run at http://localhost:8080

成功解决问题!

本作品采用《CC 协议》,转载必须注明作者和本文链接
CunWang@Ch
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!