Home >Backend Development >Golang >How to modify a value in golang

How to modify a value in golang

PHPz
PHPzOriginal
2023-04-13 18:31:441012browse

Golang is a very popular programming language. Its simple syntax, high performance and concurrency features make it widely used in web applications, network programming and other fields. In Go, modifying the value of a variable may not be easy to understand. This article will introduce in detail how to modify a value in Go.

1. Passing by value and passing by reference

In Golang, function parameters can be passed by value or by reference. Passing by value means sending a copy of the parameter value to the function. Modifications to the parameter value within the function will not affect the value outside the function. Passing by reference refers to passing parameters as pointers to functions, and modifications to the pointers within the function will be reflected outside the function.

The sample code is as follows:

package main

import "fmt"

func main() {
    var x int = 10
    fmt.Println("before call:", x)
    modifyValue(x)
    fmt.Println("after call:", x)
    modifyPointer(&x)
    fmt.Println("after call:", x)
}

func modifyValue(x int) {
    x = x + 1
    fmt.Println("in func:", x)
}

func modifyPointer(x *int) {
    *x = *x + 1
    fmt.Println("in func:", *x)
}

This code defines two functions modifyValue and modifyPointer, one for modifying the value and one for Modify the pointer. In the main function, we define an integer variable x and initialize it to 10, and then call modifyValue and modifyPointer respectively.

When we execute this program, the output result is as follows:

before call: 10
in func: 11
after call: 10
in func: 11
after call: 11

Observing the output, we can find that when modifyValue is called, although the value of the parameter is modified within the function, the function The value outside the function has not changed; when modifyPointer is called, the value pointed to by the pointer is increased by one within the function, and the value outside the function also becomes 11 accordingly. This is the difference between passing by value and passing by reference.

2. How to modify values ​​in Golang

After understanding the difference between value passing and reference passing, we can start to introduce how to modify values ​​in Golang.

2.1 Arrays and slices

In Golang, elements in arrays and slices can be accessed through indexes, and the values ​​of elements can be modified through indexes.

The sample code is as follows:

package main

import "fmt"

func main() {
    var arr [3]int = [3]int{1, 2, 3}
    fmt.Println("before modify:", arr)
    arr[1] = 4
    fmt.Println("after modify:", arr)
    
    var slc []int = []int {1, 2, 3}
    fmt.Println("before modify:", slc)
    slc[1] = 4
    fmt.Println("after modify:", slc)
}

This code defines an integer array of length 3arr and an integer sliceslc, Then modify the second element in the array and slice in sequence.

When we execute this program, the output result is as follows:

before modify: [1 2 3]
after modify: [1 4 3]
before modify: [1 2 3]
after modify: [1 4 3]

2.2 Map

In Golang, Map is a key-value pair structure, which can be mapped by key name to access and modify the values ​​in .

The sample code is as follows:

package main

import "fmt"

func main() {
    var m map[string]int = map[string]int{"apple": 1, "banana": 2, "orange": 3}
    fmt.Println("before modify:", m)
    m["banana"] = 4
    fmt.Println("after modify:", m)
}

This code defines a Map from string to integer, and then modifies the value in the Map through the key name.

When we execute this program, the output result is as follows:

before modify: map[apple:1 banana:2 orange:3]
after modify: map[apple:1 banana:4 orange:3]

2.3 Struct

In Golang, the structure is a user-defined composite type that can define the structure Body members and access and modify the values ​​of structure members through dot syntax.

The sample code is as follows:

package main

import "fmt"

type Person struct {
    Name string
    Age int
}

func main() {
    var p Person = Person{"Tom", 20}
    fmt.Println("before modify:", p)
    p.Age = 21
    fmt.Println("after modify:", p)
}

This code defines a structure type named Person, which contains a string type member Name and an integer type member Age, then define a structure variable named p and assign an initial value, and finally modify the value of the structure member.

When we execute this program, the output result is as follows:

before modify: {Tom 20}
after modify: {Tom 21}

3. Summary

Variables in Golang can be divided into basic types and composite types. Basic types cannot be modified directly, while values ​​of composite types can be modified through indexes, key names, and dot syntax. In Golang, parameter passing can be done in two ways: value passing and reference passing. You need to decide which method to use based on actual needs. In the case of value transfer, the value outside the function cannot be directly modified, and it needs to be achieved by passing a pointer.

The above is the detailed content of How to modify a value in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn