Skip to main content
updated answer after clarifications
Source Link
rob74
  • 5.2k
  • 32
  • 37

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

  • When passing parametersa 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, and currently there is nothing you can do about thatit. So if you are passing pointers to avoid copying large structs, so it is best to use this with caresparingly - 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).

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

  • When passing parameters 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 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, and currently there is nothing you can do about that, so it is best to use this with care. Some hints are given in the Go FAQ here (it refers to method receivers, but it also applies to parameters).

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).
updated answer after clarifications
Source Link
rob74
  • 5.2k
  • 32
  • 37

No - as with most programming languages, parameters in Go are usually passed by value, in which case they behave like local variables, so you can modify them inside the function, but your changes won't be visible outside the function. However, some types (e.gthis is currently not possible. pointers, slices, channels, maps)There are reference types , which means changesseveral cases to them will be visible outside of the function.distinguish:

Are you worrying about this (changes to a parameter propagating outside of a function), or is it modifying the parameter inside the function?

  • When passing parameters 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 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, and currently there is nothing you can do about that, so it is best to use this with care. Some hints are given in the Go FAQ here (it refers to method receivers, but it also applies to parameters).

No - as with most programming languages, parameters in Go are usually passed by value, in which case they behave like local variables, so you can modify them inside the function, but your changes won't be visible outside the function. However, some types (e.g. pointers, slices, channels, maps) are reference types , which means changes to them will be visible outside of the function.

Are you worrying about this (changes to a parameter propagating outside of a function), or is it modifying the parameter inside the function?

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

  • When passing parameters 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 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, and currently there is nothing you can do about that, so it is best to use this with care. Some hints are given in the Go FAQ here (it refers to method receivers, but it also applies to parameters).
Source Link
rob74
  • 5.2k
  • 32
  • 37

No - as with most programming languages, parameters in Go are usually passed by value, in which case they behave like local variables, so you can modify them inside the function, but your changes won't be visible outside the function. However, some types (e.g. pointers, slices, channels, maps) are reference types , which means changes to them will be visible outside of the function.

Are you worrying about this (changes to a parameter propagating outside of a function), or is it modifying the parameter inside the function?