Mapping
one of the important data types that go programming provides is mapping, which uniquely maps a key to a value. A key to use the object to retrieve the value at a later time. A value that can be stored in a map object given the key and value. After the value is stored, you can use its key to retrieve it.
Defining mappings
You must use the Make function to create a map.
Copy Code code as follows:
/* Declare a variable by default map would be nil*/
var map_variable map[key_data_type]value_data_type
/* Define the map as nil map can is assigned any value*/
Map_variable = Make (Map[key_data_type]value_data_type)
Example
The following examples illustrate the use of creating and mapping.
Copy Code code 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 was present otherwise entry is absent*/
if (OK) {
Fmt. Println ("Capital of United States are", capital)
}else {
Fmt. Println ("Capital of United States are not present")
}
}
Let's compile and run the above program, which will produce the following results:
Capital of India are NEW Delhi capital of France was Paris capital of Italy are Rome capital of
Japan is
tokyo
capital of United States is not present
Delete () function
The delete () function is used to delete an item from the map. mappings and corresponding keys will be deleted. Here is an example:
Copy Code code as follows:
Package Main
Import "FMT"
Func main {
/* Create a map*/
coutrycapitalmap: = map[string] string {"F" Rance ":" Paris "," Italy ":" Rome "," Japan ":" Tokyo "," India ":" New Delhi "}
fmt. PRINTLN ("Original map")
/* Print map */
for Country: = RA Nge 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: = Ran GE Countrycapitalmap {
fmt. Println ("Capital of", country, ' is ', Countrycapitalmap[country])
}
}
Let's compile and run the above program, which will produce the following results:
Original Map Capital of France was Paris capital of Italy was Rome capital of Japan was Tokyo capital of
Indi A is New Delhi
Entry for France are deleted
Updated Map capital of
India are New Delhi capital of
Italy is R Ome capital of the
Japan is Tokyo
Method
The Go programming language supports methods for special types of function calls. In the syntax of a method declaration, a "receiver" exists to represent a function in a container. The receiver can be used to invoke the function "." Operator. Here is an example:
Grammar
Copy Code code 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 for circle * *
Func (Circle Circle) area () float64 {
return Math. Pi * Circle.radius * Circle.radius
}
Func Main () {
Circle: = Circle (x:0, y:0, Radius:5)
Fmt. Printf ("Circle area:%f", Circle.area ())
}
When the above code is compiled and executed, it produces the following results: