Golang程式將專案儲存到雜湊集合中


在 Golang 中,雜湊對映是雜湊集合的一部分,它以鍵值對的形式儲存值。在這篇文章中,我們將使用兩個不同的示例將專案儲存到雜湊集合中。在第一個示例中,將使用索引將專案儲存到對映中,在第二個示例中,將使用專案結構體來儲存專案。

語法

func make ([] type, size, capacity)

Go 語言中的 make 函式用於建立陣列/對映,它接受要建立的變數的型別、大小和容量作為引數。

演算法

  • 建立一個 package main 並宣告程式中的 fmt(格式化包),其中 main 生成可執行程式碼,而 fmt 幫助格式化輸入和輸出。

  • 使用對映字面量和 make 函式建立雜湊對映,其鍵的型別為字串,值型別為 int。

  • 使用索引將值新增到雜湊對映中,例如 pencil=10、pen=20 和 scale=15。

  • 新增值後,在控制檯上列印雜湊對映。

  • 在此步驟中,我們將透過使用索引訪問鍵的值來操作雜湊對映。

  • 然後,我們將再次使用索引更新鍵的值。

  • 然後,我們將使用 delete 函式從對映中刪除特定的鍵,並在控制檯上列印更新後的對映。

  • 列印語句使用 fmt 包中的 Println() 函式執行,其中 ln 表示換行。

示例 1

在此示例中,我們將使用 make 內建函式建立一個雜湊對映,然後使用索引將所需的值新增到對映中。在這裡,我們將使用三個值,然後使用 fmt 包在終端上列印它們,並使用不同的操作來操作雜湊對映。讓我們看看程式碼和演算法以瞭解其實際工作原理。

package main
   
import "fmt"
   
func main() {
   
   // Initialize an empty map with string keys and integer values
   hashmap := make(map[string]int)
   
   // Add some items to the map
   hashmap["pencil"] = 10
   hashmap["pen"] = 20
   hashmap["scale"] = 15
   
   // Print the entire map
   fmt.Println("The hashmap created above is: ")
   fmt.Println(hashmap)
   
   // Access a specific item by its key
   fmt.Println("The value of specific element from hashmap is:")
   fmt.Println(hashmap["pencil"])
   
   // Update the value of an existing item
   hashmap["scale"] = 20
   
   // Delete an item from the map
   delete(hashmap, "pencil")
   
   // Print the updated map
   fmt.Println("The hashmap after deleting an item from it is:")
   fmt.Println(hashmap)
}

輸出

The hashmap created above is: 
map[pen:20 pencil:10 scale:15]
The value of specific element from hashmap is:
10
The hashmap after deleting an item from it is:
map[pen:20 scale:20]

示例 2

在此示例中,我們將像在最後一個示例中一樣建立一個雜湊對映,但這裡我們還將建立一個專案結構體例項來建立鍵值對,然後將這些專案新增到雜湊對映中並操作雜湊對映。輸出將使用 fmt 包列印。讓我們看看程式碼和演算法。

package main

import "fmt"

// Define a struct to represent an item
type Item struct {
   Name     string
   Quantity int 
}

func main() {
   // Initialize an empty map with string keys and Item values
   hashmap := make(map[string]Item)
   
   // Create item instances and add them to hashmap
   pen := Item{Name: "pen", Quantity: 10}
   pencil := Item{Name: "pencil", Quantity: 20}
   registers := Item{Name: "registers", Quantity: 30}
   hashmap[pen.Name] = pen
   hashmap[pencil.Name] = pencil
   hashmap[registers.Name] = registers
   
   // Print the entire map
   fmt.Println("The entire map is presented as follows: ")
   fmt.Println(hashmap)
   
   // Access a specific item by its key
   fmt.Println("The value of the element is accessed as follows:")
   fmt.Println(hashmap["pencil"])
   
   // Update the value of an existing item
   pen.Quantity = 50
   hashmap[pen.Name] = pen
   
   // Delete an item from the map
   delete(hashmap, pencil.Name)
   
   // Print the updated map
   fmt.Println("The updated map after deleting the data item is:")
   fmt.Println(hashmap)
}

輸出

The entire map is presented as follows: 
map[pen:{pen 10} pencil:{pencil 20} registers:{registers 30}]
The value of the element is accessed as follows:
{pencil 20}
The updated map after deleting the data item is:
map[pen:{pen 50} registers:{registers 30}]

結論

我們使用兩個示例執行了將專案儲存到雜湊集合中的程式。在第一個示例中,我們使用索引來儲存專案,在第二個示例中,我們使用結構體來儲存專案。

更新於: 2023年3月27日

1K+ 閱讀量

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.