Go Tricky Questions 🎯

beginner
6 min

Go Tricky Questions 🎯

Welcome to the advanced part of our Go programming tutorial! In this lesson, we'll dive into some tricky questions and concepts that might confuse beginners and even some intermediates. Let's get started! 📝

Table of Contents

  1. Functions vs Methods 1.1. Differences and similarities 1.2. Function vs Method example

  2. Pointer Types 2.1. What are pointers? 2.2. Creating and using pointers 2.3. Pointers and arrays

  3. Interfaces 3.1. What are interfaces? 3.2. Defining and using interfaces 3.3. Interface types example

  4. Quiz Time!


Functions vs Methods 📝

Functions and methods are two fundamental parts of Go programming. They differ in how they're called and the data they operate on.

Differences and similarities

  • Functions can operate on any data type, while methods are specific to a receiver type.
  • Functions are called using their name and arguments, while methods require the receiver as well as arguments.
  • Functions are global by default, while methods are methods of a specific type.

Function vs Method example

Here's a simple example of a function and a method:

go
package main import "fmt" // Function func addNumbers(a int, b int) int { return a + b } // Struct representing a rectangle type Rectangle struct { width, height int } // Method for Rectangle struct func (r Rectangle) area() int { return r.width * r.height } func main() { fmt.Println(addNumbers(2, 3)) // Output: 5 rect := Rectangle{width: 5, height: 4} fmt.Println(rect.area()) // Output: 20 }

In the above example, addNumbers is a function that takes two integers as arguments and returns their sum. area is a method for the Rectangle struct, which calculates and returns the area of a rectangle.


Pointers 📝

Pointers allow us to manipulate variables at a low level in Go. They can help optimize memory usage and performance.

What are pointers?

A pointer is a variable that stores the memory address of another variable. We can use pointers to access and modify the original variable indirectly.

Creating and using pointers

To create a pointer, we add an asterisk * before the variable name. Here's an example:

go
package main import "fmt" func main() { x := 5 px := &x fmt.Println("Value of x:", x) // Output: 5 fmt.Println("Address of x:", px) // Output: memory address of x fmt.Println("Value pointed by px:", *px) // Output: 5 *px = 10 fmt.Println("Value of x after changing through pointer:", x) // Output: 10 }

In this example, we create a pointer px that points to the variable x. We then print the value of x, the address of x, the value pointed by px, and finally change the value of x through the pointer.

Pointers and arrays

Pointers can also be used with arrays to manipulate arrays more efficiently.

go
package main import "fmt" func main() { arr := [3]int{1, 2, 3} para := &arr[0] // Pointer to the first element of the array fmt.Println("Array:", arr) // Output: [1 2 3] fmt.Println("First element of array:", arr[0]) // Output: 1 fmt.Println("Pointer to the first element:", para) // Output: memory address of first element fmt.Println("Value pointed by the pointer:", *para) // Output: 1 *para = 10 fmt.Println("Array after changing through pointer:", arr) // Output: [10 2 3] }

In this example, we create a pointer para that points to the first element of the array arr. We then print the array, the first element of the array, the pointer, and the value pointed by the pointer. We also change the value of the first element of the array through the pointer.


Interfaces 📝

Interfaces define a set of methods that a type must implement to fulfill the interface contract. They help promote polymorphism in Go.

What are interfaces?

An interface is a contract that defines a set of methods that a type must implement. A type can satisfy multiple interfaces, and an interface can be satisfied by multiple types.

Defining and using interfaces

Here's an example of defining and using an interface:

go
package main import "fmt" // Interface declaration type Shape interface { area() float64 } // Structs representing shapes that implement the Shape interface type Circle struct { radius float64 } type Rectangle struct { width, height float64 } // Implementing the Shape interface methods for each struct func (c Circle) area() float64 { return 3.14 * c.radius * c.radius } func (r Rectangle) area() float64 { return r.width * r.height } func main() { c := Circle{radius: 5} r := Rectangle{width: 4, height: 3} shapes := []Shape{c, r} for _, shape := range shapes { fmt.Println("Area of shape:", shape.area()) } }

In this example, we define an interface called Shape that requires a method called area(). We then create two structs, Circle and Rectangle, that implement the Shape interface by providing their own implementations of the area() method. In the main function, we create instances of Circle and Rectangle, add them to a slice of Shape interface type, and iterate over the slice calling the area() method for each shape.


Quiz Time! 💡

Quick Quiz
Question 1 of 1

What is the difference between a function and a method in Go?

Quick Quiz
Question 1 of 1

What is a pointer in Go, and how can we create one?

Quick Quiz
Question 1 of 1

How can we use pointers with arrays in Go?

Congratulations on making it to the end of this lesson! You now have a better understanding of functions, methods, pointers, and interfaces in Go. Keep practicing and coding, and you'll be a Go pro in no time! 🎉