In Go, when you pass a value to a function, the function works with a copy of that value. This means the original value remains unchanged after the function call.
package main import "fmt" func increaseAge(age int) { age += 5 } func main() { myAge := 10 increaseAge(myAge) fmt.Println(myAge) // Prints: 10 }
In Go, every variable has a specific location in memory. To find out where a variable is stored, you use the `&` operator to get its address. The address is displayed as a hexadecimal number.
package main import "fmt" func main() { myAge := 10 fmt.Println(&myAge) // Prints the memory address of myAge, like: 0xc000010210 }
Pointers in Go hold the address of a variable. You use the `*` operator to define a pointer and access the value at that address. For example, if you have a pointer to a variable, you can use the pointer to read or modify the variable’s value.
package main import "fmt" func main() { name := "Hello" ptr := &name // ptr holds the address of name fmt.Printf("Address of %v is %v\n", name, ptr) // Prints: Address of Hello is 0xc000010210 }
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!