Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: fmt, variadic args and %!(EXTRA type=value) error

Tags:

go

I'm implementing a wrapper around the standard log package to make a logger with various log levels.

I have the following interface:

type Logger interface {
  Trace(fmt string, args ...interface{})
  Debug(fmt string, args ...interface{})
  Info(fmt string, args ...interface{})
  Warn(fmt string, args ...interface{})
  Error(fmt string, args ...interface{})
  Fatal(fmt string, args ...interface{})
  Panic(fmt string, args ...interface{})
}

In the implementation I have something like this (not the exact code)

func Info(format string, args ...interface{}){
  msg := fmt.Sprintf(format, args...)
  log.Println(msg)
}

Now, assume I call my library like this:

logger.Info("Hello %s", "World")

I get the printout: "Hello %!(EXTRA string=WORLD)", instead of the expected "Hello World". There a similar output if I do

msg := fmt.Sprintf(format, args)

This returns "Hello World%!EXTRA []interface{}=[]".

like image 610
dialAlpha Avatar asked Oct 28 '15 19:10

dialAlpha


1 Answers

The error was between the chair and keyboard. I mixed up the following interfaces:

func Print(v ...interface{})
func Printf(format string, v ...interface{})

Some of my code was calling the library without a format string.See here for a more detailed example: http://play.golang.org/p/Xx79qujaFp

like image 69
dialAlpha Avatar answered Sep 28 '22 06:09

dialAlpha