How to leverage immutability to enhance the readability and stability of your Golang applications
The concept of immutability is very simple. Once an object (or struct) is created, it can never be changed. It is immutable. Although the concept seems simple, using it or benefiting from it is not so Easy.
As with most things in computer science (and life), there are many ways to achieve the same result, and in terms of invariance there is no difference. You should think of it as is a tool in the toolkit and is used on applicable problem scenarios. A very good use case for immutability is when you are doing concurrent programming. Golang was designed with concurrency in mind, so using concurrency in go Very common.
No matter which paradigm you use, here are some ways to use some immutability concepts in Golang to make your code more readable and stable.
This is similar to encapsulation. Create a struct with non-exported fields and export only the functions that act. Since you are only interested in the behavior of those structures, This technique is very useful for interfaces. Another good addition to this technique is to add and export a creation function (or constructor) to your structure. This way you can ensure that the state of the structure is always valid. always remains valid Can make the code more reliable because you don't have to keep dealing with invalid state for every operation you want to do with the structure. Here is a very basic example:
package amounts import "errors" type Amount struct { value int } func NewAmount(value int) (Amount, error) { if value < 0 { return Amount{}, errors.New("Invalid amount") } return Amount{value: value}, nil } func (a Amount) GetValue() int { return a.value }
In this package, we define Amount
type, has unexported field value
, constructor NewAmount
and GetValue
method for Amount
type. Once The NewAmount
function creates the Amount
structure, which cannot be changed. Therefore it is immutable from outside the package (although there are suggestions to change this in go 2, but There is no way to create immutable structures in go 1). Furthermore there are no variables of type Amount
that are in an invalid state (negative in this case), since the only way to create them already verifies this . We can call it from another package:
a, err := amounts.NewAmount(10) *// 处理错误 *log.Println(a.GetValue())
The most basic concept is to create an object (or structure body) and never change it again. But we often work on applications where entity state is important. However, the entity state and the entity's internal representation in the program are different. When using immutability, we can still assign multiple states to entities. This means that the created structure will not change, but its copy will. This does not mean that we need to manually implement the function of copying each field in the structure.
Instead, we can rely on the Go language's native behavior of copying values when calling functions. For any operation that changes the state of an entity, we can create a function that receives a structure as a parameter (or as a function receiver) and returns the changed version after execution. This is a very powerful technique because you are able to change anything on the copy without changing the variables passed as arguments by the function caller. This means no side effects and predictable behavior. If the same structure is passed to concurrent functions, each structure receives a copy of it rather than a pointer to it.
When you are using the slicing function, you will see this behavior applied to [append](https://golang.org/pkg/builtin/#append)
Function
Back to our example, let's implement the Account
type, which contains the
balance field of type
Amount
. At the same time, we add Deposit
and Withdraw
methods to change the state of the Account
entity.
package accounts import ( "errors" "my-package/amounts" ) type Account struct { balance amounts.Amount } func NewEmptyAccount() Account { amount, _ := amounts.NewAmount(0) return NewAccount(amount) } func NewAccount(amount amounts.Amount) Account { return Account{balance: amount} } func (acc Account) Deposit(amount amounts.Amount) Account { newAmount, _ := amounts.NewAmount(acc.balance.GetValue() + amount.GetValue()) acc.balance = newAmount return acc } func (acc Account) Withdraw(amount amounts.Amount) (Account, error) { newAmount, err := amounts.NewAmount(acc.balance.GetValue() - amount.GetValue()) if err != nil { return acc, errors.New("Insuficient funds") } acc.balance = newAmount return acc, nil }
If you inspect the methods we created, they will appear that we are actually changing the state of the Account
structure that is the receiver of the function. Since we are not using pointers, this is not the case, and since a copy of the struct is passed as the receiver of these functions, we will change the copy that is only valid within the function scope and then return it. Here's an example of calling it in another package:
a, err := amounts.NewAmount(10) acc := accounts.NewEmptyAccount() acc2 := acc.Deposit(a) log.Println(acc.GetBalance()) log.Println(acc2.GetBalance())
The result on the command line would be like this:
2020/06/03 22:22:40 {0} 2020/06/03 22:22:40 {10}
As you can see, despite passing the variable acc
The Deposit
method is called, but the variable does not actually change. It returns a new copy of Account
(assigned to acc2
), which contains the changed field.
使用指针具有优于复制值的优点,特别是如果您的结构很大时,在复制时可能会导致性能问题,但是您应始终问自己是否值得,不要尝试过早地优化代码。尤其是在使用并发时。您可能会在一些糟糕的情况下结束。
不变性不仅可以应用于结构,还可以应用于函数。如果我们用相同的参数两次执行相同的函数,我们应该收到相同的结果,对吗?好吧,如果我们依赖于外部状态或全局变量,则可能并非总是如此。最好避免这种情况。有几种方法可以实现这一目标。
如果您在函数内部使用共享的全局变量,请考虑将该值作为参数传递,而不是直接在函数内部使用。 那会使您的函数更可预测,也更易于测试。整个代码的可读性也会更容易,其他人也将会了解到值可能会影响函数行为,因为它是一个参数,而这就是参数的用途。 这里有一个例子:
package main import ( "fmt" "time" ) var rand int = 0 func main() { rand = time.Now().Second() + 1 fmt.Println(sum(1, 2)) } func sum(a, b int) int { return a + b + rand }
这个函数 sum
使用全局变量作为自己计算的一部分。 从函数签名来看这不是很清楚。 更好的方法是将rand变量作为参数传递。 因此该函数看起来应该像这样:
func sum(a, b, rand **int**) **int** { return a + b + rand }
推荐教程:《Go教程》
The above is the detailed content of Detailed explanation of immutable types in Go. For more information, please follow other related articles on the PHP Chinese website!