Problems encountered in the command line execution go program under non project directory

Problems encountered in the command line execution go program under non project directory

Problem discovery

When we run our project, we compile our project into a binary executable file through go build. There is no problem executing this executable file in the current directory

However, if we cut other directories and execute the executable file through the path, an error will be reported! Because we start the project to read a configuration file about the project environment, we can't find this configuration file in [/ users / codetop / setup], because after compilation, the configuration file found in the program is the configuration file found relative to the current execution directory, not based on the project directory structure (there may be no directory structure at this time)

How to solve - through command line parameter injection

Method 1 use os.Args

Previously, our configuration file is read through viper, so we inject the path of the configuration file through the command line, so we don't look for it based on the project

First, we print os.Args during viper initialization

    InitAppConfig Initialize vipper's configuration file
func InitAppConfig() (error error) {

After build ing code


Cut out the directory that is not the current project and execute it (the error is still reported, but you can see the print result of os.Args = > [/ users / codetop / remote es server code / go-cli-v1 / go-cli-v1])

Then we can inject the path of the configuration file by passing in the second parameter

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

If you find this, you can run successfully!

Method 2 standard library flag

Standard library flag document

//flag.Type(flag name, default value, help information) * type (defines a data type and returns the corresponding pointer type)
configPath := flag.String("config", "", "Project profile") 

flag is defined. You can view help information by running the project -h

configPath := flag.String("config", "", "Project profile")

Execution: configuration can be injected in this way

viper.AddConfigPath(*configPath) //Set configuration file path

Full version

$~/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/go-cli-v1 -config /Users/codehope/remote-es-server-code/go-cli-v1/config]
  Database initialization succeeded!
  redis Initialization succeeded!
  [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

Successfully solve the problem!

Tags: Go

Posted on Thu, 25 Nov 2021 20:18:29 -0500 by getDizzle