SoFunction
Updated on 2025-03-03

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

Mapping
GoAn important data type provided by programming is mapping,Uniquely map a key to a value。 An object to be used to retrieve the value later. Given keys and values,Can be in oneMapThe value of the object store。 After the value is stored, you can use its key to retrieve it.

Define the map
A map must be created using the make function.

Copy the codeThe 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)


example
The following examples illustrate the use of creation and mapping.

Copy the codeThe 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 {
      ("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){
      ("Capital of United States is", capital) 
   }else {
      ("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 map and the corresponding keys will be deleted. Here is an example:

Copy the codeThe 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"}
  
   ("Original map")  
  
   /* print map */
   for country := range countryCapitalMap {
      ("Capital of",country,"is",countryCapitalMap[country])
   }
  
   /* delete an entry */
   delete(countryCapitalMap,"France");
   ("Entry for France is deleted") 
  
   ("Updated map")  
  
   /* print map */
   for country := range countryCapitalMap {
      ("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

method
The Go programming language supports methods of special types of function calls. In the syntax of method declaration, the "receiver" exists to represent functions in the container. This receiver can be used to call the function "." operator. Here is an example:

grammar

Copy the codeThe 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 * *
}

func main(){
   circle := Circle(x:0, y:0, radius:5)
   ("Circle area: %f", ())
}


When the above code is compiled and executed, it produces the following results:
Circle area: 78.539816