golang函数中的常量参数

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

Constant Parameter in golang function

问题

在Go语言中,函数的参数是值传递的,这意味着函数内部对参数的修改不会影响到函数外部的原始值。因此,你不需要显式地将参数标记为常量来防止意外修改。如果你希望确保函数内部不会修改参数的值,可以通过将参数声明为指针类型,并在函数内部使用指针来操作参数的方式来实现。这样可以避免不必要的副本,并确保函数内部对参数的修改不会影响到函数外部的原始值。

英文:

I am new to the golang. Is it possible to mark a parameter as constant in function ?
So that the parameter is not modified accidentally.

答案1

得分: 34

不,目前还不可能实现。有几种情况需要区分:

  • 当以“正常”方式传递参数时,即按值传递,你不必担心修改它,因为这些参数的行为类似于局部变量,所以你可以在函数内部修改它们,但是你的更改在函数外部不可见。但是,这个规则有一个例外...

  • ...一些 Go 类型(例如指针、切片、通道、映射)是引用类型,这意味着对它们的更改将在函数外部可见。一些细节在这里给出1

  • 你可以将指针(例如指向结构体的指针)作为参数传递,这样更改将在函数外部可见。如果这不是你的意图,目前没有什么办法可以解决。因此,如果你传递指针是为了避免复制大型结构体,最好谨慎使用——记住,“过早优化是万恶之源”。Go FAQ 中给出了一些提示2(它涉及方法接收器,但也适用于参数)。

英文:

No, this is currently not possible. There are several cases to distinguish:

  • When passing a parameter "normally", i.e. by value, you don't have to worry about modifying it, since these parameters behave like local variables, so you can modify them inside the function, but your changes won't be visible outside the function. But, there is an exception to this rule...

  • ...some Go types (e.g. pointers, slices, channels, maps) are reference types, which means changes to them will be visible outside of the function. Some details are given here.

  • You can pass pointers (e.g., to structs) as parameters, in which case changes will be visible outside the function. If this is not intended, currently there is nothing you can do about it. So if you are passing pointers to avoid copying large structs, it is best to use this sparingly - remember, "Premature optimization is the root of all evil". Some hints are given in the Go FAQ here (it refers to method receivers, but it also applies to parameters).

答案2

得分: 7

不可以。

你可以在函数体内声明一个常量,但不能将其作为参数。

在Go语言中,常量在编译时创建,且永远不会改变,而函数参数必须在每次调用时根据运行时的情况进行更改。

英文:

No.

You can declare a constant inside a function body, but not as a parameter.

In Go, constants are created in compile time and can never change, while function parameters must change with each call at run time.

答案3

得分: 4

传递给常量参数的值仍然有一个方便的应用程序:您无法无意中更改初始值。

考虑以下代码:

func Generate(count int) (value []byte) {
   value = make([]byte, count)
   for i:=0; i<count; count++ {
       value[i] = byte(i) // 仅作为示例
   }
   return
}

这是有效的Go代码,在编译过程中没有警告或错误。这种类型的拼写错误可能很难追踪。

英文:

There's still a handy application to the const parameter passed by value: you can't unintentionally change the initial value.

Consider following code:

func Generate(count int) (value []byte) {
   value = make([]byte, count)
   for i:=0; i&lt;count; count++ {
       value[i] = byte(i) // just for an example
   }
   return
}

This is a valid Go code, no warning or errors during the compilation. Such kind of typo might be painful to track.

答案4

得分: 0

一个常量可以在函数定义的范围内定义。

const 变量名 变量类型
func myFunction() {
    ...
}

函数的参数不能是常量,因为它必须是一个可变的变量,才能在函数内部进行更改。
如果一个函数参数是常量,我们需要将其值放入一个变量中才能对其进行操作。

英文:

A constant can be defined in the scope where the function has been defined.

const varName varType
func myFunction() {
    ...
}

Parameters of a function cannot be constant because it have to be a variable to be changeable inside the function.
If a function parameter was constant, we needed to put its value in a variable to do operations on it.

huangapple
  • 本文由 发表于 2014年2月28日 17:40:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/22091368.html
匿名

发表评论

匿名网友

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

确定