Go – 无法将其用作赋值的类型

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

Go - Cannot use as type in assignment

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是Go语言的新手。

我对为什么仍然出现这个错误消息感到困惑。
> cannot use cmd.Args() (type cli.Args) as type CmdArgs in assignment

错误消息解释了cmd.Args() (type cli.Args)不能赋值给type CmdArgs,其中type CmdArgscli.Args类型。

我已经阅读了https://stackoverflow.com/questions/43900806/cannot-use-as-type-in-assignment-in-go,但是它并没有让我理解我的错误在哪里。我认为这与我的问题不同。

请问有什么解决方案吗?

这是我的代码。

package main

import (
	"fmt"
	"os"

	"github.com/urfave/cli"
)

// CmdArgs是命令参数
type CmdArgs cli.Args

func main() {
	program := cli.NewApp()
	program.Action = func(cmd *cli.Context) error {
		var args CmdArgs
		args = cmd.Args()
               ▼▼▼▼▼▼▼▼▼
               cannot use cmd.Args() (type cli.Args) as type CmdArgs in assignment

		return nil
	}

	program.Run(os.Args)
}

谢谢你的关注。

英文:

I'm new in Go Lang.

I confused why this error message still coming.
> cannot use cmd.Args() (type cli.Args) as type CmdArgs in assignment

The error message explain that cmd.Args() (type cli.Args) cannot assignment to type CmdArgs which is type CmdArgs is cli.Args.

I have read https://stackoverflow.com/questions/43900806/cannot-use-as-type-in-assignment-in-go, but it does not make me understand where's my wrong is. I think that's a different matter with me.

Any solution please?

Here's my code.

package main

import (
	"fmt"
	"os"

	"github.com/urfave/cli"
)

// CmdArgs is command arguments
type CmdArgs cli.Args

func main() {
	program := cli.NewApp()
	program.Action = func(cmd *cli.Context) error {
		var args CmdArgs
		args = cmd.Args()
               ▼▼▼▼▼▼▼▼▼
               cannot use cmd.Args() (type cli.Args) as type CmdArgs in assignment

		return nil
	}

	program.Run(os.Args)
}

Thanks for your attention.

答案1

得分: 1

错误的意思就是它所说的:你试图将函数的返回值赋给一个与返回值类型不同的变量,这是无效的。当你定义一个新类型时,它是一个新类型,不能直接赋值。你可以进行类型转换,但是在Go语言中没有隐式类型转换 - 转换必须显式地进行:

var args CmdArgs
normalArgs := cmd.Args()
args = CmdArgs(normalArgs)

不过我不禁要问你为什么要创建一个名为CmdArgs的新类型,但我假设在代码示例中没有显示的原因。你可能会发现嵌入类型比起别名更容易一些。

英文:

The error means exactly what it says: you're trying to assign the return value of a function to a variable that's of a different type than the return value, which is invalid. When you define a new type, it's a new type, and not directly assignable. You can cast between them, but there is no implicit casting in Go - the cast must be done explicitly:

    var args CmdArgs
    normalArgs := cmd.Args()
    args = CmdArgs(normalArgs)

Though I have to wonder why you're creating a new type CmdArgs to begin with, but I assume there's some reason that's not indicated in the code example. You might have an easier time embedding rather than aliasing, however.

答案2

得分: 1

args是一个类型为CmdArgs的变量,它具有一个类型为cli.Args的变量。

将你的函数改为:

package main

import (
    "fmt"
    "os"

    "github.com/urfave/cli"
)

func main() {
    program := cli.NewApp()
    program.Action = func(cmd *cli.Context) error {
        args := cmd.Args()
        return nil
    }

    program.Run(os.Args)
}

然后它应该可以运行。

英文:

args is a variable of type CmdArgs which has a vairable of type cli.Args

Change your function to

package main

import (
    "fmt"
    "os"

    "github.com/urfave/cli"
)

func main() {
    program := cli.NewApp()
    program.Action = func(cmd *cli.Context) error {
        args := cmd.Args()
        return nil
    }

    program.Run(os.Args)
}

and it should run.

huangapple
  • 本文由 发表于 2017年7月3日 21:52:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/44887356.html
匿名

发表评论

匿名网友

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

确定