Self Built Utility package cannot access variables beyond main

176 views
Skip to first unread message

Zhaoxun Yan

unread,
Jun 6, 2025, 6:30:49 AM6/6/25
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, 9:19:25 AM6/6/25
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

Zhaoxun Yan

unread,
Jun 9, 2025, 12:35:17 AM6/9/25
to golang-nuts
it turned out to be an error misjudgement. The real line that caused trouble is this:
s = er.Error()+string(out)
The error is gone after I changed it to this line :
s = fmt.Sprintf("%v %s", er, out)

Kurtis Rader

unread,
Jun 9, 2025, 1:50:53 AM6/9/25
to Zhaoxun Yan, golang-nuts
On Sun, Jun 8, 2025 at 9:35 PM Zhaoxun Yan <yan.z...@gmail.com> wrote:
it turned out to be an error misjudgement. The real line that caused trouble is this:
s = er.Error()+string(out)
The error is gone after I changed it to this line :
s = fmt.Sprintf("%v %s", er, out)

The stdlib os.exec.Command() function only returns a single value. Which should result in a compiler error since your program expects two values. I'm going to guess that you are calling a different function with that name which can return two values and the "er" return value can be nil. Which would result in the "er.Error()" call to dereference a nil value. So I think you have incorrectly diagnosed the cause of the problem. The first statement does not correctly handle "er" being nil. The "fmt.Sprintf()" correctly handles "er" being the nil value.
 
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CADEX6_Wzy%3DPoqdXBNUqQ0tGYmQfacTq6A%2By58a5osDkKB-Z1yA%40mail.gmail.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Zhaoxun Yan

unread,
Jun 9, 2025, 10:02:21 PM6/9/25
to Kurtis Rader, golang-nuts
Oh! Thank you Kurtis. So do you have a special Editor to inform you about the functions? My current editor cannot even tell what type of variable a go function would return, let alone parameter hints, etc.

Kurtis Rader

unread,
Jun 9, 2025, 10:23:56 PM6/9/25
to Zhaoxun Yan, golang-nuts
On Mon, Jun 9, 2025 at 7:01 PM Zhaoxun Yan <yan.z...@gmail.com> wrote:
Oh! Thank you Kurtis. So do you have a special Editor to inform you about the functions? My current editor cannot even tell what type of variable a go function would return, let alone parameter hints, etc.

I use a VIM plugin that talks to the https://pkg.go.dev/golang.org/x/tools/gopls tool. That makes it easy to jump to the definition of a function or other symbol and find all the places that use it. It is likely your editor can be configured to interact with that tool.

Robert Engels

unread,
Jun 9, 2025, 10:29:17 PM6/9/25
to Kurtis Rader, Zhaoxun Yan, golang-nuts
Just use VSCode with the standard Go plugin. 

On Jun 9, 2025, at 9:23 PM, Kurtis Rader <kra...@skepticism.us> wrote:



Zhaoxun Yan

unread,
Jun 10, 2025, 2:17:25 AM6/10/25
to Kurtis Rader, golang-nuts
No, the function does return two variables, as I take out "er" from "out, err:="
go vet report a flaw:
/util.go:51:9: cannot initialize 1 variables with 2 values
Reply all
Reply to author
Forward
0 new messages