Comments in Go are used to document your code and are ignored by the compiler. There are two types of comments: single-line and multi-line.
// This is a single-line comment /* This is a multi-line comment */
Go supports generating documentation from comments in the code. You can use the `go doc` command in the terminal to learn more about packages and functions. For example, to learn about the `fmt` package:
$ go doc fmt
To use functions from different packages, you need to import them into your Go file. You can either list each package separately or group them together using parentheses.
import "fmt" import "math" // Or, using grouped import: import ( "fmt" "math" )
Go is a compiled language, meaning you must compile your source files into an executable program. You can compile and run Go files using the `go run` or `go build` commands.
$ go run main.go $ go build main.go
A package in Go is a collection of related Go files. To use code from a specific package, you need to import it into your Go program.
import "fmt"
In Go, values can be either literals (like `3.14`, `true`, or `"Hello"`) or variables. A variable is a named storage that can hold a value that may change during the program execution.
// literal value fmt.Println("PI =", 3.14) // named variable var name = "John"
Every value in Go has a data type that defines the kind of data it represents, such as integers, strings, or booleans. Knowing the data type is essential for proper memory management.
var age int = 30 var name string = "Alice"
Variables in Go can be declared and initialized in several ways. You can declare a variable without an initial value, with a default zero value, or explicitly initialize it.
var number int // default value is 0 number = 42 // short-hand declaration age := 25
Errors in Go are raised when the code does not follow the language's rules. These errors are displayed in the terminal with specific details to help identify and fix the issue.
// Example error: undefined variable fmt.Println(dinner) // error: undefined: dinner
If a variable is declared without an initial value, it takes a default zero value. Each data type has its own zero value, such as `0` for integers, `""` for strings, and `false` for booleans.
var isActive bool fmt.Println(isActive) // false
The `fmt` package provides functions like `Print()` and `Println()` for displaying output in the terminal. `Print()` concatenates arguments without spacing, while `Println()` adds spaces and a newline.
fmt.Print("Hello", "World") // HelloWorld fmt.Println("Hello", "World") // Hello World
`Printf()` allows formatted output using verbs like `%v` for values, `%d` for integers, and `%s` for strings. It is useful for custom string formatting.
name := "John" fmt.Printf("My name is %v", name) // My name is John
The `Scan()` function reads input from the user and assigns it to specified variables. It is used for interactive programs that require user input.
var name string fmt.Print("Enter your name: ") fmt.Scan(&name) fmt.Println("Hello,", name)
An `if` statement evaluates a condition and executes a block of code if the condition is `true`. It helps make decisions in a program based on dynamic conditions.
if temperature > 30 { fmt.Println("It's hot outside!") }
An `else` statement executes a block of code when the `if` condition is `false`. You can use `else if` to evaluate additional conditions.
if temperature > 30 { fmt.Println("It's hot outside!") } else if temperature < 10 { fmt.Println("It's cold outside!") } else { fmt.Println("The weather is nice.") }
Go supports comparison operators (like `==`, `!=`, `<`, `>`) and logical operators (`&&`, `||`, `!`) to evaluate conditions.
if age >= 18 && isStudent { fmt.Println("You are an adult student.") }
`Switch` statements provide an alternative to multiple `if-else` statements by allowing a value to be tested against a list of cases.
switch day { case "Monday": fmt.Println("Start of the work week.") case "Friday": fmt.Println("End of the work week.") default: fmt.Println("Midweek.") }
Functions in Go can accept parameters, which are variables passed into them. These parameters can be manipulated inside the function without affecting the original value (pass by value).
func increment(number int) int { return number + 1 } fmt.Println(increment(5)) // Output: 6
A pointer holds the memory address of a variable. In Go, you can use the `&` operator to get a variable's address and `*` to dereference it (access the value at the address).
var x int = 10 var p *int = &x fmt.Println(*p) // Output: 10
Using pointers in function parameters allows the function to modify the original variable's value. This is useful when you want to update a variable's value from within a function.
func updateValue(val *int) { *val = 20 } num := 10 updateValue(#) fmt.Println(num) // Output: 20
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!