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)
CodePudding user response:
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"}
CodePudding user response:
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)
CodePudding user response:
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)