search
HomeBackend DevelopmentGolangDetailed explanation of immutable types in Go

Detailed explanation of immutable types in Go

Immutability in Golang

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.

Only Export the functionality of a struct without exporting its fields

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 <p>In this package, we define <code>Amount</code> type, has unexported field <code>value</code>, constructor <code>NewAmount</code> and <code>GetValue</code> method for <code>Amount</code> type. Once The <code>NewAmount</code> function creates the <code>Amount</code> 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 <code>Amount</code> 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: </p><pre class="brush:php;toolbar:false">a, err := amounts.NewAmount(10)
*// 处理错误
*log.Println(a.GetValue())

Use value copy instead of pointer in function

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!

Statement
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete
Golang vs. Python: The Pros and ConsGolang vs. Python: The Pros and ConsApr 21, 2025 am 12:17 AM

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang and C  : Concurrency vs. Raw SpeedGolang and C : Concurrency vs. Raw SpeedApr 21, 2025 am 12:16 AM

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Why Use Golang? Benefits and Advantages ExplainedWhy Use Golang? Benefits and Advantages ExplainedApr 21, 2025 am 12:15 AM

Reasons for choosing Golang include: 1) high concurrency performance, 2) static type system, 3) garbage collection mechanism, 4) rich standard libraries and ecosystems, which make it an ideal choice for developing efficient and reliable software.

Golang vs. C  : Performance and Speed ComparisonGolang vs. C : Performance and Speed ComparisonApr 21, 2025 am 12:13 AM

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

Is Golang Faster Than C  ? Exploring the LimitsIs Golang Faster Than C ? Exploring the LimitsApr 20, 2025 am 12:19 AM

Golang performs better in compilation time and concurrent processing, while C has more advantages in running speed and memory management. 1.Golang has fast compilation speed and is suitable for rapid development. 2.C runs fast and is suitable for performance-critical applications. 3. Golang is simple and efficient in concurrent processing, suitable for concurrent programming. 4.C Manual memory management provides higher performance, but increases development complexity.

Golang: From Web Services to System ProgrammingGolang: From Web Services to System ProgrammingApr 20, 2025 am 12:18 AM

Golang's application in web services and system programming is mainly reflected in its simplicity, efficiency and concurrency. 1) In web services, Golang supports the creation of high-performance web applications and APIs through powerful HTTP libraries and concurrent processing capabilities. 2) In system programming, Golang uses features close to hardware and compatibility with C language to be suitable for operating system development and embedded systems.

Golang vs. C  : Benchmarks and Real-World PerformanceGolang vs. C : Benchmarks and Real-World PerformanceApr 20, 2025 am 12:18 AM

Golang and C have their own advantages and disadvantages in performance comparison: 1. Golang is suitable for high concurrency and rapid development, but garbage collection may affect performance; 2.C provides higher performance and hardware control, but has high development complexity. When making a choice, you need to consider project requirements and team skills in a comprehensive way.

Golang vs. Python: A Comparative AnalysisGolang vs. Python: A Comparative AnalysisApr 20, 2025 am 12:17 AM

Golang is suitable for high-performance and concurrent programming scenarios, while Python is suitable for rapid development and data processing. 1.Golang emphasizes simplicity and efficiency, and is suitable for back-end services and microservices. 2. Python is known for its concise syntax and rich libraries, suitable for data science and machine learning.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool