SoFunction
Updated on 2025-03-05

Methods used in golang

background

The map data structure in go is not thread-safe, that is, multiple goroutines operate a map at the same time, an error will be reported, so go1.9 was born

The idea comes from the ConcurrentHashMap of java

interface

It is the thread-safe map brought by version 1.9, and there are mainly the following methods:

Load(key interface{}) (value interface{}, ok bool)
//By providing a key key, find the corresponding value value, and if it does not exist, return nil.  The result of ok indicates whether the value is found in the map
Store(key, value interface{})
//This is equivalent to writing a map (update or add). The first parameter is the key and the second parameter is the value.
LoadOrStore(key, value interface{}) (actual interface{}, loaded bool)
//By providing a key key, find the corresponding value value, if there is an existing value of the return key, otherwise store and return the given value, return true if it is read, return false if it is stored
Delete(key interface{})
//Delete the corresponding value of the key by providing a key key
Range(f func(key, value interface{}) bool)
//Loop to read the value in the map.//becausefor ... range mapIt is a built-in language feature,So there is no way to usefor rangeTraversal, But you can use itRangemethod,Through callback

practice

package main

import (
	"fmt"
	"sync"
)

var num = 0
var addTest *AddTest

func init() {
	addTest = &AddTest{}
}

type AddTest struct {
	m 
}

func (at *AddTest) increment(wg *) {
	//Mutual Exclusion Lock	() //When a thread goes in and adds a lock	num++
	() //Unlock it after it comes out, and other threads can enter	()
}

func (at *AddTest) decrement(wg *) {
	//Mutual Exclusion Lock	() //When a thread goes in and adds a lock	num--
	() //Unlock it after it comes out, and other threads can enter	()
}

var w 

var aa map[int]int

func main() {
	var bb 
	var wg 
	//aa = make(map[int]int)
	(2)
	go func() {
		//(1)
		for i:=0 ;i <100; i++{
			//aa[i] = i+1
			//("a")
			(i, i+1)
		}
		()
	}()

	go func() {

		for i:=0 ;i <100; i++{
			//aa[i] = i+1
			//("a")
			(i, i+1)
		}
		()
	}()
	()
	(func(k, v interface{}) bool {
		("iterate:", k, v)
		return true
	}
}

Summarize

  • Read and write lock and mutex lock Read and write lock: You can acquire multiple read locks, only read and write conflicts (when you add a read lock, other threads cannot write) Mutex lock: has nothing to do with read and write operations. If you add a lock, the resources in the lock will be exclusive to the thread.
  • I personally feel that it is not convenient to use, so it is better to use it yourself to have a mutex according to the actual scenario. For example, maps are all readable, and only serial execution is required when writing, so the write operation can encapsulate the mutex lock.
  • Due to the many internal operations, it is not suitable for scenarios with large-scale writing (suitable for large-scale reading and small-scale writing).
  • For details of the principle, please refer to:https:///article/

refer to

/liupengjie/go/718991

/2017/07/11/dive-into-sync-Map/

This is all about this article about the use of golang. For more related content in golang, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!