Tag Archives: group

Remember the error of using unaddressable value of a novice goer

Go test, report an error;

Error in go test panic: reflect: reflect. Value. Set using addressable value [recovered].

Solution: generally, there is an error of using unaddressable value, which indicates that the pointer value passed is incorrect. For example, if the pointer address needs to be passed, but the value is passed. At that time, I had a hunch that DB should have passed a non pointer when finding. After verification, it was.

So, the summary is: when updating in Gorm, you can not pass the pointer (if you want to use the returned primary key, you still need to pass the pointer), but when querying, you must pass it, even if it is slice or map type.

[go] solve the fatal error of go: concurrent map writes map non concurrent security

Map is not concurrency safe, when there are multiple concurrent growths reading and writing the same map  
A panic error occurs

concurrent map writes

For example, this error occurs in the following code:

var mMap map[int]int

func TestMyMap(t *testing.T) {
    mMap = make(map[int]int)

    for i := 0; i < 5000; i++ {
        go func() {
            mMap[i] = i
        }()
        go readMap(i)
    }
}
func readMap(i int) int {
    return mMap[i]
}

There are many ways to solve this problem. Now we use read-write lock,

Concurrent access to map is not safe, and undefined behavior will appear, leading to program exit. Therefore, if you want to access the map concurrently in multiple coroutines, you must provide some synchronization mechanism. Generally, you can control the concurrent access to the map by reading and writing the lock sync.rwmutex. Encapsulating the map and sync.rwmutex can realize the secure concurrent access to the map

Code after transformation

type SMap struct {
    sync.RWMutex
    Map map[int]int
}

func (l *SMap) readMap(key int) (int, bool) {
    l.RLock()
    value, ok := l.Map[key]
    l.RUnlock()
    return value, ok
}

func (l *SMap) writeMap(key int, value int) {
    l.Lock()
    l.Map[key] = value
    l.Unlock()
}

var mMap *SMap

func TestMyMap(t *testing.T) {
    mMap = &SMap{
        Map: make(map[int]int),
    }

    for i := 0; i < 5000; i++ {
        go func() {
            mMap.writeMap(i, i)
        }()
        go readMap(i)
    }
}
func readMap(i int) (int, bool) {
    return mMap.readMap(i)
}

  There are three ways:

1. Use channel
2. Use sync. Map
3. Use map but lock it

WSS connection server error

Error:

1. The error prompt for SSL certificate rejection is different between Firefox and chrome

(1) Chrome error: websocket connection failed: error in connection establishment: Net:: err_ CERT_ AUTHORITY_ INVALID

(2) an error is reported in Firefox: it is unable to create a wss://www.wss.com/ Connection to the server.

2. Although the error prompts for SSL certificate rejection are different between Firefox and chrome, the solution steps are exactly the same.

 

code:

1 var ws = new WebSocket("wss://www.wss.com");

 

Cause of the problem:

Because the certificate is self signed, the CA of the certificate must not exist in the root storage area of the operating system. Naturally, the operating system will not recognize you, and the natural browser will not recognize you, that is, the self signed certificate is not trusted.

 

Solution:

1. Open a new tab page in Firefox or chrome.

2. Visit your websocket server domain name: https://www.wss.com (change the WSS request to an HTTPS request with the same domain name and port number).

3. You will find the browser alarm: “your connection is not private connection…”.

Don’t panic, look down and click “advanced”.

5. Continue to click “continue to” www.wss.com (unsafe) “.

6. The page will prompt “400 bad request…”, don’t worry. This is due to using HTTP protocol to access WSS service. Don’t worry. You can solve the prompt error here.

 

Reprinted from: http://www.blogdaren.com/post-2456.html?from=singlemessage

Reproduced in: https://www.cnblogs.com/XuYuFan/p/10917909.html