--------------------
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x6646f2]
goroutine 1 [running]:
util.CommandArgs({0xc000093cb0?, 0x2?, 0xc000071380?})
/home/zxun/src/util/util.go:49 +0xf2
--------------------
This error occurred because package [main] uses a function called "CommandArgs" from my self-built package [util] with args in another package [conf] in the same directory. Package [util] got lost because it does not know where is package [conf]
like this
[util] /home/zxun/src/util.go
[conf] /home/zxun/src/conf.go
[main] /home/zxun/src/main.go...
/home/zxun/src/go.mod:
require(
util v0.0.0
...
replace util => ../util
The usage line in main.go:
fmt.Println( util.CommandArgs([]string{conf.IreportExec, conf.IreportScript, "redis"}) )
util.CommandArgs:
func CommandArgs(args []string) string{
if len(args) ==0{
return "args are empty!"
}
var s string
if len(args) ==1{
out, er := exec.Command(args[0]).CombinedOutput()
s = er.Error()+string(out)
}else{
out, er := exec.Command(args[0],args[1:]...).CombinedOutput()
s = er.Error()+string(out)
fmt.Println(s)
} return s
}
So is my analysis correct? I have not seen s printed yet, because s shall include "<nil>" at the beginning. I am using go1.18. If the cause is util package cannot access a variable in conf, is it possible to walk around by a deepcopy of the string array (which will be in package main)?
Thanks in advance.