将golang转换为void类型

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

golang casting to void type

问题

我知道在Go语言中,无返回值函数是不需要明确声明的,可以直接写成:

func foo(start int, end int) { ... }

那么在C语言中,有没有类似的强制转换为void的概念,以强调返回值不被使用呢?

(void) printf("hello world");

在Go语言中是否存在这样的概念(或等价的概念)呢?

英文:

I know that void functions in go are written without mentioning it:

func foo(start int, end int) { ... }

What about the notion of casting to void in C, to emphasize returned value is not used:

(void) printf("hello world");

does this notion (or equivalent) exist in go?

答案1

得分: 3

Go语言不支持类型转换,也没有void类型,因此没有与您所要求的功能等效的方法。

为了记录您有意忽略返回值的函数的目的,您可以使用空白标识符来丢弃该值:

_, _ = fmt.Printf("Hello world")
英文:

Go doesn't support type casting, and Go doesn't have any void type, so there's no functional equivalent to what you're asking for.

For the purpose of documenting that you're intentionally ignoring the return value of a function that returns something, you can use the blank identifier, which simply discards the value:

_, _ = fmt.Printf("Hello world")

答案2

得分: 2

在Go语言中不存在这个概念(或等价概念)。

英文:

> [D] oes this notion (or equivalent) exist in go?

No.

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

发表评论

匿名网友

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

确定