Go Keywords Reference šŸš€

beginner
5 min

Go Keywords Reference šŸš€

Welcome to our deep dive into the world of Go (Golang)! In this lesson, we'll explore various Go keywords that are essential for every programmer. By the end, you'll have a solid understanding of these keywords and how they help structure your Go programs.

Let's get started! šŸŽÆ

Basic Keywords šŸ“

Go uses several keywords to define different program elements. Here's a list of basic keywords:

  1. package - Organizes code into manageable packages
  2. import - Imports packages for use in the current package
  3. const - Declares constant variables
  4. var - Declares variable variables
  5. type - Defines custom data types
  6. func - Declares functions
  7. defer - Defers the execution of a function call until the surrounding function returns
  8. if - Conditional statement
  9. else - Alternate condition in an if statement
  10. switch - Multiple conditional statements
  11. case - Conditions in a switch statement
  12. for - Loop construct
  13. range - Loop through collections like arrays, slices, or maps
  14. break - Exits a loop
  15. continue - Skips to the next iteration of a loop
  16. fallthrough - Forces execution of subsequent case statements in a switch
  17. goto - Jumps to a labeled statement
  18. return - Terminates a function and returns control to the calling function
  19. nil - Represents the absence of a value

šŸ’” Pro Tip: Go has no private or public access modifiers. Instead, it uses package-level visibility for global variables and functions, and the private concept is achieved by naming them with a leading underscore (e.g., _variableName).

Quiz šŸ“

Quick Quiz
Question 1 of 1

Which keyword is used to organize code into manageable packages?

Advanced Keywords šŸš€

As you progress in your Go journey, you'll encounter more advanced keywords:

  1. make - Allocates and initializes memory for collections like arrays, slices, and maps
  2. new - Allocates memory for a specific data type without initializing it
  3. map - A collection of key-value pairs
  4. interface - Defines a type with a set of methods that must be implemented by any type that implements the interface
  5. struct - Defines custom composite data types
  6. func type - Defines a new function type
  7. chan - Communication channels between concurrent Go routines
  8. select - Used for communication with multiple channels

šŸ’” Pro Tip: Remember to use the make function when creating slices and maps, as it automatically initializes the underlying memory.

Quiz šŸ“

Quick Quiz
Question 1 of 1

Which keyword is used to allocate and initialize memory for a slice?

Putting It All Together šŸ’”

Now that you've learned about the essential Go keywords, let's put them into practice with some working examples.

Example 1 - Defining a Function šŸ“

go
package main import "fmt" func greet(name string) string { return "Hello, " + name } func main() { message := greet("Alice") fmt.Println(message) }

Example 2 - Using a Map šŸ“

go
package main import "fmt" func main() { scores := make(map[string]int) scores["Alice"] = 90 scores["Bob"] = 85 scores["Charlie"] = 80 for name, score := range scores { fmt.Printf("Name: %s, Score: %d\n", name, score) } }

Remember to run these examples using the go run command in your terminal.

Conclusion āœ…

In this lesson, we've explored various Go keywords and gained a deeper understanding of their purposes and uses. Practice these keywords in your own projects, and you'll be well on your way to mastering Go! 🄳

Happy coding! šŸš€