On Sun, Jun 8, 2025 at 9:35 PM Zhaoxun Yan <yan.zhao...@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() <https://pkg.go.dev/os/exec@go1.24.4#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. > On Fri, Jun 6, 2025 at 6:29 PM Zhaoxun Yan <yan.zhao...@gmail.com> wrote: > >> -------------------- >> 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. >> > -- > 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+unsubscr...@googlegroups.com. > To view this discussion visit > https://groups.google.com/d/msgid/golang-nuts/CADEX6_Wzy%3DPoqdXBNUqQ0tGYmQfacTq6A%2By58a5osDkKB-Z1yA%40mail.gmail.com > <https://groups.google.com/d/msgid/golang-nuts/CADEX6_Wzy%3DPoqdXBNUqQ0tGYmQfacTq6A%2By58a5osDkKB-Z1yA%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank -- 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+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD8Yh6nMAy9xLeuKZMTWssrj_teX%2B_xdJnc7RxisTZrp8A%40mail.gmail.com.