Understanding control flow constructs in Go, Learning about functions and their applications, including closures and recursion
full lessons here👇:
https://programmerscareer.com/golang-basic-skill/
Generated by AI, there may be errors, for reference only
Topic: For, If Else, Switch
Control flow is a crucial aspect of all programming languages. It determines the order in which your code gets executed. In Golang, these are primarily if-else
, for
, and switch
.
For loop:
The for
loop is the only looping construct present in Go. Here’s what a basic for loop looks like:
1 | for i := 0; i < 10; i++ { |
This for
loop prints the numbers from 0 to 9.
If-Else construct:
The if
statement allows your program to conditionally execute code. You can combine multiple if statements with else if
or else
.
1 | num := 10 |
This if-else
construct prints out whether a number is even or odd.
Switch Case construct:
A switch
statement is a shorter way to write a sequence of if - else
statements. It runs the first case whose value is equal to the condition expression. Here’s an example:
1 | fruit := "Apple" |
This switch case
construct prints out a statement based on the value of the fruit
variable.
Topic: Functions
Now let’s dive into functions in Go!
Function Declaration:
Functions in Go are defined with the func
keyword. The following is the general syntax for declaring a function:
1 | func functionName(parameterName parameterType) returnType { |
For example, here’s a simple function that takes in two integers and returns their sum:
1 | func addNumbers(num1 int, num2 int) int { |
Function Arguments:
Our addNumbers
function takes two arguments, num1
and num2
, that are both of type int
. In Go, function arguments are provided in parentheses following the function name.
Return Types:
Our addNumbers
function also returns a value of type int
. The return type of a function is specified after the function parameters and before the function body. The return
statement is used inside the function body to specify the result to be returned.
Topic: Using range
in Go
range
in Go is a keyword used in for loop to iterate over items of an array, slice, string or map.
Range in Arrays:
Here’s how you can use range
with an array:
1 | numbers := [6]int{10, 20, 30, 40, 50, 60} |
In this example, range
returns two values: the index and the value at that index.
Range in Slices:
range
works similarly for slices. If you had a slice instead of an array in the above example, the code would still work the same.
Range in Maps:
When used with maps, range
returns the key and the value. Here’s an example:
1 | m := make(map[string]int) |
range
provides a simple and intuitive way to iterate over arrays, slices, and maps in Go.
Topic: Closures in Go
Closures are a special case of anonymous functions. Anonymous functions can form closures by capturing variables from the surrounding scope.
Defining Closures:
In Go, closures are defined right at the place where you need them, and optionally, they can be assigned to a variable or immediately invoked. Here is a simple example:
1 | func main() { |
In this example, we define an anonymous function that takes two integers and returns their sum. We immediately assign this function to the sum
variable, making sum
a-function-which-is a closure.
Understanding Closures:
Closures have access to variables from their surrounding scope. Here is a simple closure that keeps state:
1 | func counter() func() int { |
In this example, the counter
function returns another function, which forms a closure over the count
variable. Each time this returned function is called, it increments count
and returns its value. This count
variable persists across multiple calls to the count()
function but is not accessible outside of it.
Topic: More on Closures in Go
Let’s dive deeper into the concept of closures in Go. It’s important to note that Go supports closures, which means that a function can have an ‘enclosed’ environment holding its own variables.
Consider following example:
1 | package main |
In this example, we call intSeq()
, assigning the result (a function) to nextInt
. This nextInt
function value captures its own i
value, which will be updated each time we call nextInt
.
To confirm that the state is unique to that particular function, create and test a new one.
The output after running this program will be
1 | 1 |
This characteristic of function values lets us use them in more interesting ways, such as the line nextInt := intSeq()
. Depending on what you’re doing, using closures with encapsulated data can give your programs good structure.
Topic: Recursion in Go
Recursion is the process in which a function calls itself directly or indirectly. The corresponding function is called as a recursive function.
Understanding Recursion:
Let’s take an example of a simple recursive function in Go:
1 | package main |
In this function, the recurse
function prints then calls itself with a decrementing counter until the counter becomes 0. Each nested call has its own count
variable, and they do not interfere with each other.
Factorial Recursive Function:
The factorial function is a classic example of recursion. In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.
Here is a factorial implementation using recursion in Go:
1 | package main |
In this factorial
function, it calls itself multiplying n
by the factorial of n-1
until it gets down to 0, at which point it stops calling itself and starts unwinding, returning the computed values.
Keep in mind, while recursion can solve problems in a clear and concise manner, excessive or careless use can lead to a stack overflow error due to excessive function calls. Always ensure your recursive functions have a well defined and reachable base case.
中文文章: https://programmerscareer.com/zh-cn/go-basic-03/
Author: Wesley Wei – Twitter Wesley Wei – Medium
Note: If you choose to repost or use this article, please cite the original source.
Comments