How to access after flag.Args()

huangapple go评论75阅读模式
英文:

How to access after flag.Args()

问题

我的应用程序是一个从网易下载的大型应用程序。它在一开始就遇到了问题。

func main() {
	if len(flag.Args()) == 0 {
		logger.Error.Fatal("Missing music address:(")
	}
}

编译后,我得到了 ncm-dl.exe,当我运行 ./ncm-dl.exe xxxxx 时,我得到了以下错误信息:

[Error] 2021/08/31 22:06:59 Missing music address:(

我想知道运行时出了什么问题。

这是我的日志记录器包。

package logger

import (
	"os"
	"log"
)

var (
	Error = log.New(os.Stderr, "[Error] ", log.LstdFlags)
)
英文:

My app is a huge application for download from netease.It meets problem at first.

func main() {
	if len(flag.Args()) == 0 {
		logger.Error.Fatal("Missing music address:(")
	}
}

After compiling,I get ncm-dl.exe and when I run ./ncm-dl.exe xxxxx.I get

[Error] 2021/08/31 22:06:59 Missing music address:(

I want to know what's up when I run.

Here is my logger package.

package logger

import (
	"os"
	"log"
)

var (
	Error   = log.New(os.Stderr, "[Error] ", log.LstdFlags)
)

答案1

得分: 1

你需要在进行任何操作之前调用flag.Parse

package main
import "flag"

func main() {
   flag.Parse()
   if flag.NArg() == 0 {
      println("fail")
      return
   }
   println("pass")
}

https://godocs.io/flag#Parse

英文:

You need to call flag.Parse before doing anything:

package main
import "flag"

func main() {
   flag.Parse()
   if flag.NArg() == 0 {
      println("fail")
      return
   }
   println("pass")
}

https://godocs.io/flag#Parse

huangapple
  • 本文由 发表于 2021年8月31日 22:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/69000429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定