In Go, you can create an empty map using the `make` function. You need to specify what type of keys and values the map will hold.
myMap := make(map[string]int) // Creates an empty map with string keys and int values
You can also initialize a map with some key-value pairs right when you create it.
myMap := map[string]int{ "a": 1, "b": 2, "c": 3, } // Creates a map with initial values
To get a value from a map, use its key. You can also check if a key exists and get a boolean result.
value, exists := myMap["b"] // Retrieves value for key "b" and checks if it exists
You can add new key-value pairs to a map by specifying the key and the value.
myMap["d"] = 4 // Adds a new key-value pair to the map
Update an existing value by assigning a new value to its key.
myMap["a"] = 10 // Updates the value for key "a" to 10
To remove a key-value pair from a map, use the `delete` function with the map and the key you want to remove.
delete(myMap, "c") // Removes the key-value pair with key "c"
A map in Go is a collection of key-value pairs where each key is unique. It's used to quickly find data using a key.
myMap := map[string]string{ "name": "Alice", "city": "Wonderland", } // Example map with string keys and values
A struct is a way to group related variables together. Each variable in a struct is called a field.
type Person struct{ Name string Age int } // Defines a struct with a name and age
Define a struct by naming it and listing its fields and their types.
type Car struct{ Brand string Year int } // Defines a Car struct with brand and year fields
Create an instance of a struct by using its name and providing values for its fields.
myCar := Car{Brand: "Toyota", Year: 2022} // Creates a Car instance
You can add functions to structs, which operate on the struct’s fields. These functions are called methods.
func (p Person) Greet() string { return "Hello, " + p.Name } // Method to greet using a Person struct
Access fields in a struct using the dot `.` operator.
fmt.Println(myCar.Brand) // Prints the brand of the car
To modify a struct’s fields in a function, pass the struct as a pointer. This way, changes affect the original struct.
func (p *Person) HaveBirthday() { p.Age++ } p1 := Person{Name: "Bob", Age: 30} p1.HaveBirthday() // p1.Age is now 31
You can access fields of a struct pointer without explicitly dereferencing it, using the dot `.` syntax.
pointerToCar := &myCar fmt.Println(pointerToCar.Year) // Prints the year of the car
You can use arrays to store multiple instances of the same struct type.
people := []Person{ {Name: "Alice", Age: 30}, {Name: "Bob", Age: 25}, } // Array of Person structs
A struct can contain other structs as fields, allowing for more complex data structures.
type Address struct { Street string City string } type Person struct { Name string Address Address } // Struct with a nested Address struct
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!