-2

I'm trying to debug my golang app. Currently, I have an API request that isn't working which has this line of code: fmt.Errorf("Object(%q).CopierFrom(%q).Run: %v", dstName, object, err)

How can I view the output of this error log? And if it's not possible what are some other ways to debug in go? (runtime invocation would be nice)

Steez
  • 219
  • 5
  • 17
  • 3
    If you want to log an error, you have to log it. `fmt.Errorf` just creates an error object. An error has no output. – Jonathan Hall Dec 29 '21 at 16:11
  • If you'll update your question with some actual code, it will be easier to provide a concrete suggestion. But maybe you want to call `fmt.Printf` or `log.Error` or something like that instead. – Jonathan Hall Dec 29 '21 at 16:12

4 Answers4

2

fmt.Errorf creates an error object; it does not print.

From the docs for fmt.Errorf:

func Errorf(format string, a ...interface{}) error

If you're just trying to print the message to stdout:

fmt.Printf("Object(%q).CopierFrom(%q).Run: %v\n", dstName, object, err)

If you want to write to an error log, I recommend looking at the log package. For example, if you were looking to write to stderr:

logger := log.New(os.Stderr, "my-app", 0)
logger.Printf("Object(%q).CopierFrom(%q).Run: %v", dstName, object, err)
xgord
  • 4,606
  • 6
  • 30
  • 51
2

fmt.Errorf() creates an error object. but not print.doc

If you're just trying to print the message to stdout: run

package main

import (
    "fmt"
)

func main() {
    const name, id = "bueller", 17
    err := fmt.Errorf("user %q (id %d) not found", name, id)
    fmt.Println(err.Error())

}

out:

user "bueller" (id 17) not found

if you want debug golang code, I recommend use log packages for example: zerolog


package main

import (
    "errors"

    "github.com/rs/zerolog"
    "github.com/rs/zerolog/log"
)

func main() {
    // UNIX Time is faster and smaller than most timestamps
    zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

    err := errors.New("seems we have an error here")
    log.Error().Err(err).Msg("this is an error")
}

out:

{"level":"error","error":"seems we have an error here","time":1640795128,"message":"this is an error"}
s4eedb
  • 46
  • 4
1

fmt.Errorf creates an error - ideal for function returns - but it's not implicitly logged.

If you want to log an error simply:

log.Printf("api X: error %v", err)
colm.anseo
  • 19,337
  • 4
  • 43
  • 52
0

It is better to read the function signature and comments before you use any functions.

// Errorf formats according to a format specifier and returns the string as a
// value that satisfies error.
//
// If the format specifier includes a %w verb with an error operand,
// the returned error will implement an Unwrap method returning the operand. It is
// invalid to include more than one %w verb or to supply it with an operand
// that does not implement the error interface. The %w verb is otherwise
// a synonym for %v.
func Errorf(format string, a ...interface{}) error 
Changsong Li
  • 143
  • 6