Tag Archives: Golang Error

[Solved] Golang Error: main.go:5:2: package go_code/chapter10/factory/model is not in GOROOT

golang Error: is not in GOROOT

The following errors are reported during operation

PS C:\goproject\src\go_code\chapter10\factory\main> go run main.go
main.go:5:2: package go_code/chapter10/factory/model is not in GOROOT (C:\go\src\go_code\chapter10\factory\model)

The process is as follows

PS C:\goproject\src\go_code\chapter10\factory\main> go run main.go
main.go:5:2: package go_code/chapter10/factory/model is not in GOROOT (C:\go\src\go_code\chapter10\factory\model)

PS C:\goproject\src\go_code\chapter10\factory\main>  go env
set GO111MODULE=

PS C:\goproject\src\go_code\chapter10\factory\main> go env -w GO111MODULE=off
PS C:\goproject\src\go_code\chapter10\factory\main> go run main.go
{tom 78.9}

[Solved] Golang Error: fatal error: concurrent map writes

The specific codes are as follows:

package main

import (
	"fmt"
	"time"
)

var m = make(map[int]int, 10)

func solution(n int){
	res := 1
	for i:=1; i<=n; i++{
		res = res * i
	}
	m[n] = res
}

func main(){
	for i:=1; i<=200; i++{
		go solution(i)
	}
	time.Sleep(time.Second*10)
	for ind, val := range m{
		fmt.Printf("[%d] = %d \n", ind, val)
	}
}

The following error occurred:

fatal error: concurrent map writes
fatal error: concurrent map writes




runtime.mapassign_fast64(0x10b7760, 0xc00001e1b0, 0x12, 0x0)
        /usr/local/go/src/runtime/map_fast64.go:176 +0x325 fp=0xc000106fa0 sp=0xc000106f60 pc=0x1010bc5
main.solution(0x12)
        /Users/lcq/go/src/go_base/gochanneldemo/channeldemo.go:15 +0x65 fp=0xc000106fd8 sp=0xc000106fa0 pc=0x10a88a5
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000106fe0 sp=0xc000106fd8 pc=0x1062c41
created by main.main
        /Users/lcq/go/src/go_base/gochanneldemo/channeldemo.go:20 +0x58

The main reason is because map is not thread-safe, so it is not safe to use map in case of concurrency.
Solution.

    1. Add a lock
    2. Use sync.map
    3. Use a channel (multiple threads operating a channel is thread-safe)

Golang Error: fatal error: concurrent map read and map writ

The reason for the error is that the map is thread unsafe and an error will be reported when there are concurrent reads and writes
solution: use the read-write lock sync.rwmutex:
golang read-write lock
rules
when the read lock exists, other read locks can exist together, but the write lock needs to wait
when the write lock exists, both the read lock and the write lock need to wait
the read lock can be concurrent, and the read lock and the write lock are mutually exclusive, Write lock and write lock are mutually exclusive

var rw sync.RWMutex
//Read lock and locking
rw.Rlock()
//Read lock and unlocking
rw.Runlock()

[Solved] Golang Error: The system cannot find the path specified. [mkdir C:/xx/yy/]:

The system cannot find the path specified

If you report an error when creating a directory, it is recommended that you check the following:

err := os.Mkdir(path, perm)

If you use the above method, you can only create a single level directory
if you want to create a directory such as XX/YY, XX/YY/ZZ,…
please enter the following method

err := os.MkdirAll(path, perm)

OK! Problem solved!

[Go] Solve panic: runtime error: invalid memory address or nil pointer dereference in golang

When a property or method is called on nil, a null pointer will be reported

Especially the structure pointer, this problem is very easy to occur, the following is the test code

package tools

import "fmt"

func MyTest() {
    type MConn struct {
        Name string
    }
    var conn * MConn
    var conn2 MConn
    conn3 := new (MConn)
    conn4 := & MConn{}
    fmt.Printf("%v,%v,%v,%v" , conn, conn2, conn3, conn4)
}

Return separately

<nil>,{},&{},&{}

When a structure pointer variable var conn *MConn is declared, but it is not initialized and the property is called directly, it will appear

panic: runtime error: invalid memory address or nil pointer dereference

Because conn is nil at this time, it is a null pointer

A null operation must be performed, if conn != nil {}

 

Of course, we sometimes do not make such an obvious error, but when we cooperate with map, this error may occur unintentionally.

    var mMap map[ string ]* MConn
    m1: = mMap[ " name " ]
    m1.Name = " qqq "

In this code map, when the key element does not exist, the zero value of value is returned, which happens to be *MConn. The zero value is nil, and an error will also be reported.

So the map has to be judged here

    var mMap map[ string ]* MConn
    m1, ok: = mMap[ " name " ]
     if ok {
        m1.Name = " qqq " 
    }