Self Built Utility package cannot access variables beyond main

43 views
Skip to first unread message

Zhaoxun Yan

unread,
Jun 6, 2025, 1:30:49 PM (2 days ago) Jun 6
to golang-nuts
--------------------
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.

Brian Candler

unread,
Jun 6, 2025, 4:19:25 PM (2 days ago) Jun 6
to golang-nuts
So is my analysis correct?

I don't think so, but you've hidden the important parts of the backtrace which would show exactly where the error is.  Try trimming your program down to the *minimum* which reproduces the problem, then post the *entire* reproducer so we can run it and see the problem for ourselves.

I am using go1.18

Go 1.18 support ended 2 years and 4 months ago, according to https://endoflife.date/go

Reply all
Reply to author
Forward
0 new messages