A brief analysis of the basic use of mapping and methods in Go language programming

  • 2020-05-30 20:20:26
  • OfStack

mapping
One important data type provided by Go programming is the mapping, where only 1 maps 1 key to 1 value. 1 key to use the object to retrieve the value later. Given the key and value, the value can be stored in an Map object. Once the value is stored, you can retrieve it using its key.

Define the mapping
You must use the make function to create a mapping.


/* 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)

example
The following example illustrates the use of creation and mapping.


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 Delhi
Capital of France is Paris
Capital of Italy is 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. The mapping and corresponding keys will be deleted. Here's an example:


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 Map
Capital of France is Paris
Capital of Italy is Rome
Capital of Japan is Tokyo
Capital of India is New Delhi
Entry for France is deleted
Updated Map
Capital of India is New Delhi
Capital of Italy is Rome
Capital of Japan is Tokyo

methods
The Go programming language supports methods for special types of function calls. In the syntax of method declarations, "sinks" exist to represent functions in a container. This sink can be used by calling the function ". "operator. Here's an example:

grammar


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(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:


Circle area: 78.539816


Related articles: