Key Concepts

Review core concepts you need to learn to master this subject

Go Pass by Value Parameter

When a Go function parameter is passed by value, it means only a copy of the value is accessed and manipulated inside the function. The original value of the variable that is passed as an argument to the function remains intact.

func makeMeOlder(age int) { age += 5 } func main() { myAge := 10 makeMeOlder(myAge) fmt.Println(myAge) // myAge is still 10 }

Go Variable Address

A Go variable occupies a slot in virtual memory and is accessible via an address. To access the address of a variable, type & followed by the variable name. The value of a variable address is in the form of a hexadecimal number, such as 0x414020.

name := "Codecademy" fmt.Printf("Address of %v is %v", name, &name) // Address of Codecademy is 0xc000010210

Go Pointer Dereferencing

The * operator preceding a data type is describing the data type for a Go pointer. For example:

var pointerToInt *int // a pointer to a variable of type int

The * operator preceding a variable is used to dereference a pointer variable. For example, the pointer variable, x, is assigned the address of variable, y. We dereference x by typing *x. By doing so, we can access and change the value of y. For example:

y := 3 var x *int = &y *x = 5 fmt.Println(y) // y is now 5
Chevron Left Icon
Learn Go: Functions
Lesson 1 of 2
Chevron Right Icon
  1. 1
    In programming, a function is a block of code designed to be reused. As programmers, we want to find solutions to problems, but we also don’t want to do additional work when it’s not necessary. Let…
  2. 2
    As we saw before, we can bundle code in a function and call that function when we need the code inside it to run. We’ll be going over function syntax starting with a simple function definition: fu…
  3. 3
    A function definition creates something called a scope. We’ve referenced scope before in our [conditionals exercise about scoped short declarations](…
  4. 4
    While variables and their values are scoped to their functions, we do have built-in ways of passing information out of their native functions and into another namespace. Let’s describe the way that…
  5. 5
    We know that functions can return information, but we can also provide functions with information using parameters. Function parameters are variables that are used within the function to use in s…
  6. 6
    Functions are great for code reuse, this means that when you find yourself repeating the same pattern over and over, it might be a good idea to try and abstract it into a function. When you abstr…
  7. 7
    Functions also have to ability to return multiple values. Check out the example below: func GPA(midtermGrade float32, finalGrade float32) (string, float32) { averageGrade := (midtermGrade + fina…
  8. 8
    We can delay a function call to the end of the current scope by using the defer keyword. defer tells Go to run a function, but at the end of the current function. This is useful for logging, file w…
  9. 9
    Congrats! This lesson covered how to: Define a function. Understand scope rules. Return a value from a function. Add parameters to our function. Reuse code with functions. Return multi…
  1. 1
    Go is a pass-by-value language. In other words, we’re passing functions the value of an argument. In a technical sense, when we’re calling a function with an argument, the Go compiler is stri…
  2. 2
    Picture being in class and taking part in a lesson. When we hear an important detail, we write it down in our notebook for reference later. This same idea of storing important information somewhere…
  3. 3
    In the previous exercise we went over addresses, now let’s learn how to store them. In Go, pointers do that job for us. Pointers are variables that specifically store addresses. We even set the …
  4. 4
    We know that addresses are where values are stored and pointers allow us to keep track of addresses. But what if we want the address to store a different value? Well, we can actually use our pointe…
  5. 5
    Using our knowledge of addresses, pointers, and dereferencing, let’s return to our initial problem: How can we change the value of a variable when we’re in a different scope? Let’s take a look at t…
  6. 6
    Congrats! You can now change variable values even when the variables are out of scope! The following concepts were covered in this lesson: Go is a pass-by-value language. An address is where …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo