为什么以下的导入语句不起作用?

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

Why isn't the following import working?

问题

为什么以下代码可以正常工作:

package main

import (
	"os"

	cli "github.com/urfave/cli"
)

func main() {
	cli.NewApp().Run(os.Args)
}

但是当我按照 https://github.com/urfave/cli 的建议将 cli 的导入更改为以下内容时:

import (
	"os"

	cli "gopkg.in/urfave/cli.v2"
)

就会出现错误 undefined: cli.NewApp

英文:

Why the following works

package main

import (
	"os"

	cli "github.com/urfave/cli"
)

func main() {
	cli.NewApp().Run(os.Args)
}

but when I change the cli import to following as suggested in https://github.com/urfave/cli

import (
    	"os"
    
    	cli "gopkg.in/urfave/cli.v2"
    )

It gives this error undefined: cli.NewApp

答案1

得分: 5

v2版本的包没有NewApp()方法。

英文:

v2 of the package has no NewApp() method.

答案2

得分: 1

由于它不使用默认值进行初始化,所以下面的示例与NewApp()方法不完全相同,但如果你想尝试一下包的v2版本,可以尝试类似这样的代码。

package main

import (
	"os"

	cli "gopkg.in/urfave/cli.v2"
)

func main() {
	(&cli.App{}).Run(os.Args)
}

请确保阅读v2包中包含的README.md文件,因为它还包含更新的说明和示例。

英文:

As it doesn't initialize with defaults the example below is not exactly the same as the NewApp() method, but you can try something like this, if you want to give v2 of the package a try.

package main

import (
	"os"

	cli "gopkg.in/urfave/cli.v2"
)

func main() {
	(&cli.App{}).Run(os.Args)
}

Make sure to read the README.md file contained in the v2 package, as it also contains updated instructions and examples.

huangapple
  • 本文由 发表于 2016年12月9日 10:33:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/41052220.html
匿名

发表评论

匿名网友

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

确定