The basic usage of Mapping and method in Go language programming
This article shows you the basic use of mapping and methods in GE language programming. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Mapping
An important data type provided by Go programming is mapping, which uniquely maps a key to a value. A key to use the object that retrieves the value later. Given keys and values, values can be stored in a Map object. After the value is stored, you can use its key to retrieve it.
Define Mappin
You must use the make function to create a mapping.
The copy code is as follows:
/ * declare a variable, by default map will be nil*/
Var map_variable map[key _ data_type] value_data_type
/ * define the map as nil map can not be assigned any value*/
Map_variable = make (map [key _ data_type] value_data_type)
Examples
The following example illustrates the use of creation and mapping.
The copy code is as follows:
Package main
Import "fmt"
Func main {
Var coutryCapitalMap map[string] string
/ * create a map*/
CoutryCapitalMap = make (map [string] string)
/ * insert key-value pairs in the map*/
CountryCapitalMap ["France"] = "Paris"
CountryCapitalMap ["Italy"] = "Rome"
CountryCapitalMap ["Japan"] = "Tokyo"
CountryCapitalMap ["India"] = "New Delhi"
/ * print map using keys*/
For country: = range countryCapitalMap {
Fmt.Println ("Capital of", country, "is", countryCapitalMap [country])
}
/ * test if entry is present in the map or not*/
Captial, ok: = countryCapitalMap ["United States"]
/ * if ok is true, entry is present otherwise entry is absent*/
If (ok) {
Fmt.Println ("Capital of United States is", capital)
} else {
Fmt.Println ("Capital of United States is not present")
}
}
Let's compile and run the above program, which will produce the following results:
Capital of India is New DelhiCapital of France is ParisCapital of Italy is RomeCapital of Japan is TokyoCapital of United States is not present
Delete () function
The delete () function is used to remove an item from the mapping. The mapping and the corresponding keys will be deleted. Here is an example:
The copy code is as follows:
Package main
Import "fmt"
Func main {
/ * create a map*/
CoutryCapitalMap: = map [string] string {"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New Delhi"}
Fmt.Println ("Original map")
/ * print map * /
For country: = range countryCapitalMap {
Fmt.Println ("Capital of", country, "is", countryCapitalMap [country])
}
/ * delete an entry * /
Delete (countryCapitalMap, "France")
Fmt.Println ("Entry for France is deleted")
Fmt.Println ("Updated map")
/ * print map * /
For country: = range countryCapitalMap {
Fmt.Println ("Capital of", country, "is", countryCapitalMap [country])
}
}
Let's compile and run the above program, which will produce the following results:
Original MapCapital of France is ParisCapital of Italy is RomeCapital of Japan is TokyoCapital of India is New DelhiEntry for France is deletedUpdated MapCapital of India is New DelhiCapital of Italy is RomeCapital of Japan is Tokyo
Method
The Go programming language supports methods for special types of function calls. In the syntax of a method declaration, the "sink" exists to represent the function in the container. The receiver can be used by calling the function "." Operator. Here is an example:
Grammar
The copy code is as follows:
Func (variable_name variable_data_type) function_name () [return_type] {
/ * function body*/
}
Package main
Import (
"fmt"
"math"
)
/ * define a circle * /
Type Circle strut {
X,y,radius float64
}
/ * define a method for circle * /
Func (circle Circle) area () float64 {
Return math.Pi * circle.radius * circle.radius
}
Func main () {
Circle: = Circle (XRO, YRO, radius:5)
Fmt.Printf ("Circle area:% f", circle.area ())
}
When the above code is compiled and executed, it produces the following results:
Circle area: 78.539816 the above is the basic use of mapping and methods in Go language programming. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.