Home Backend Development Golang How are Golang function parameters passed?

How are Golang function parameters passed?

Apr 12, 2024 am 08:54 AM
golang Function parameter passing

In Go, there are three ways to pass function parameters: pass by value (value or copy), pass by reference (pointer), and pass by immutable reference (structure/array reference, unmodifiable object). When passed by value, the function operates on a copy of the value and does not affect the original value; when passed by reference, the function can modify the original value; when passed by immutable reference, the function can operate on the fields of the object, but does not modify the object itself. These three methods are suitable for different scenarios. For example, passing by value is suitable for read-only operations, passing by reference is suitable for modification operations, and passing by immutable reference is suitable for operating fields of structures/arrays.

How are Golang function parameters passed?

Go function parameter passing mechanism

In the Go language, function parameters are passed in three main ways:

  • Pass by value (call by value): Pass a value (or a copy of the value) to the function.
  • Pass by reference (call by reference): Pass a pointer to the value to the function so that the function can modify the original value.
  • Pass by immutable reference (call by value of immutable reference): Pass an immutable reference of a structure or array to the function, so that the function cannot modify the original object, but can modify it Field (if the structure or array has one).

Pass by value

When you pass a parameter by value, a copy of the parameter value is passed. Any modification to this value only affects the copy within the function, not the original value.

func modifyValue(num int) {
    num *= 2
}

func main() {
    x := 10
    modifyValue(x)
    fmt.Println(x) // 输出:10
}
Copy after login

Pass by reference

If you want to allow a function to modify the passed value, you can use pointer pass by reference. This is accomplished by declaring a pointer parameter using the * prefix.

func modifyReference(num *int) {
    *num *= 2
}

func main() {
    x := 10
    modifyReference(&x)
    fmt.Println(x) // 输出:20
}
Copy after login

Passing by immutable reference

Structures and arrays can be passed by passing their immutable reference. This allows the function to access the fields of the structure or array, but not modify the object itself.

type Point struct {
    x int
    y int
}

func modifyPoint(p Point) {
    p.x *= 2
}

func main() {
    point := Point{10, 20}
    modifyPoint(point)
    fmt.Println(point) // 输出:{20 20}
}
Copy after login

Practical case

Suppose we have a function calculateTotalCost, its function is to calculate their total cost given a list of items.

import "fmt"

type Item struct {
    name  string
    price float64
}

func calculateTotalCost(items []Item) float64 {
    total := 0.0
    for _, item := range items {
        total += item.price
    }
    return total
}

func main() {
    items := []Item{
        {"Item 1", 10.00},
        {"Item 2", 20.00},
        {"Item 3", 30.00},
    }

    totalCost := calculateTotalCost(items)
    fmt.Println(totalCost) // 输出:60.00
}
Copy after login

In this case, items is passed as a slice by value. This means that the function will receive a copy of the items variable without modifying the original slice.

The above is the detailed content of How are Golang function parameters passed?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

How to safely read and write files using Golang?

How to configure connection pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

How to configure connection pool for Golang database connection?

Similarities and Differences between Golang and C++ Similarities and Differences between Golang and C++ Jun 05, 2024 pm 06:12 PM

Similarities and Differences between Golang and C++

How steep is the learning curve of golang framework architecture? How steep is the learning curve of golang framework architecture? Jun 05, 2024 pm 06:59 PM

How steep is the learning curve of golang framework architecture?

How to generate random elements from list in Golang? How to generate random elements from list in Golang? Jun 05, 2024 pm 04:28 PM

How to generate random elements from list in Golang?

Comparison of advantages and disadvantages of golang framework Comparison of advantages and disadvantages of golang framework Jun 05, 2024 pm 09:32 PM

Comparison of advantages and disadvantages of golang framework

What are the best practices for error handling in Golang framework? What are the best practices for error handling in Golang framework? Jun 05, 2024 pm 10:39 PM

What are the best practices for error handling in Golang framework?

golang framework document usage instructions golang framework document usage instructions Jun 05, 2024 pm 06:04 PM

golang framework document usage instructions

See all articles