Function arguments in Golang
Go supports two main ways to pass arguments: Pass by Value and Pass by Reference. Go uses pass by value by default.
In Go, defer statements delay execution of a function or method or an anonymous method until the surrounding function returns. In other words, the arguments to the defer function or method are evaluated immediately, but they do not execute until the surrounding function returns. You can create a deferred method, function, or anonymous function using the defer keyword.
Syntax:
// Hàm
defer func func_name(parameter_list Type)return_type{
// Code
}
// Phương thức
defer func (receiver Type) method_name(parameter_list){
// Code
}
defer func (parameter_list)(return_type){
// code
}()
Important Note:
Now let's look at an example to understand better.
Example 1:
// Chương trình Go minh họa
// khái niệm của lệnh defer
package main
import "fmt"
// Các hàm
func mul(a1, a2 int) int {
res := a1 * a2
fmt.Println("Result: ", res)
return 0
}
func show() {
fmt.Println("Hello!, Quantrimang.com")
}
// Hàm chính
func main() {
// Gọi hàm mul()
// Tại đây hàm mul hoạt động
// như một hàm bình thường
mul(23, 45)
// Gọi hàm mul()
// Dùng từ khóa defer
// Hàm the mul()
// là hàm defer
defer mul(23, 56)
// Gọi hàm show()
show()
}
Result:
Result: 1035
Hello!, Quantrimang.com
Result: 1288
Explanation: In the above example, we have two functions named mul() and show() . While the show() function is called normally in the main() function , the mul() function is called in two different ways:
Example 2:
// Minh họa chương trình Go
// dùng nhiều lệnh defer, để minh họa chính sách LIFO
package main
import "fmt"
// Các hàm
func add(a1, a2 int) int {
res := a1 + a2
fmt.Println("Result: ", res)
return 0
}
// Hàm chính
func main() {
fmt.Println("Start")
// Nhiều lệnh defer
// Triển khai theo thứ tự LIFO
defer fmt.Println("End")
defer add(34, 56)
defer add(10, 10)
}
Result:
Start
Result: 20
Result: 90
End
Go supports two main ways to pass arguments: Pass by Value and Pass by Reference. Go uses pass by value by default.
In Go language, you are allowed to return multiple values from a function, using the return statement. In other words, in a function, a return statement can return multiple values.
Golang like most other programming languages has switch statement. Here is how to use switch statement in Golang.
In this article, we will learn how to use default case to avoid deadlock. But first, we will learn what is deadlock case when using select command in Golang?
Anonymous structs in Golang are temporary structures with no names used for one-time purposes, while anonymous fields allow embedding of unnamed fields.
What is Rune in Golang? How to use Rune in Golang? This article will give you the answer.
Operators allow us to perform different types of operations on operands. In Go language, operators can be classified based on their different functions.
_(underscore) in Golang is called Blank Identifier. Identifier is a user-defined name of program elements used for identification purposes.
Hello, World! is the first basic program in any programming language. You can write this first program in Golang by following the steps below.
Golang can be easily installed on Windows. Here is a step-by-step guide to install Golang on Windows.
Golang allows creating two or more methods with the same name in the same package, but the receivers of these methods must be of different types.
In Go, functions are blocks of code that perform specific tasks, which can be reused throughout the program to save memory, improve readability, and save time.
The scope of a variable in Golang determines which part of the program the variable is accessible to. In Go, all identifiers have a lexical scope that is determined at compile time.
What conditional statements does Golang have? How to use conditional statements in Go programming? Let's find out together!
The short variable declaration operator (:=) in Golang is used to create variables with a proper name and initial value. Here is what you need to know about the short variable declaration operator in Golang.
Windows 10 has built-in video and image editor right on your computer with simple customizations so you can process videos without needing video editing software.
Mind maps are extremely useful when we need to remember information because the content is designed in the most visual style. Below are instructions for creating a mind profile in Word.
With iOS 18, Apple has made it even easier to customize Control Center and even your lock screen with support for widgets from third-party apps.
Google launched the Pixel 9 Pro Fold at its Made by Google event in August 2024. While it has a few improvements over its predecessor to compete with the Galaxy Z Fold 6 and OnePlus Open, these are the features that many people are most excited to try.
Different World: Light and Magic gives players a series of attractive rewards including Star Diamonds, Sparkling Stardust, EXP, Soul Stones, and Summon Cards in the giftcode.
Get the Ninja Legend giftcode now to receive attractive rewards.
These seemingly normal actions can cause damage to your home in the long run. Here are some things to avoid to keep your home neat, clean and durable.
Microsoft has finally officially announced the planned end of support (EOS) for the Office 2016 and Office 2019 application ecosystem.
When choosing to buy a new phone, the first thing many people often do is refer to the specification table. This is basically not wrong, however, there are some details that you should not pay too much attention to because they no longer reflect the actual experience.
Microsoft Office has supported ActiveX for years as an option for document extensibility and automation, but it's also a serious security vulnerability.
MIUI 12.5 will be based on Android 11 and also comes with additional features in Xiaomi smartphones.
To ensure that files are deleted permanently, you can use SDelete. This command line utility securely deletes data, making it unrecoverable.
Thanks to the rapid development of the artificial intelligence (AI) field, coupled with the huge demand for graphics cards (GPUs) and neural processing units (NPUs), the semiconductor industry has recorded impressive revenue growth.
Samsung is one of the smartphone manufacturers that offers the longest software support period for its devices, especially its flagships.
It probably won't surprise you to learn that Facebook, Instagram, and WhatsApp are top targets for hackers and scammers, but there are a few ways to protect your account.