Comments (2)
The function signature of MarshalKeyvals
is as follows.
func MarshalKeyvals(keyvals ...interface{}) ([]byte, error)
That is, it accepts a variable number of interface{}
arguments. It can be called in one of two ways.
Either explicitly listing a sequence of keys and values
logfmt.MarshalKeyvals("app", "myapp", "env", "prod", "error", "errormsg")
Or expanding a pre-existing slice in place, like so.
kvs := []interface{}{"app", "myapp", "env", "prod", "error", "errormsg"}
logfmt.MarshalKeyvals(kvs...)
(See also the section in the Go spec entitled Passing arguments to ... parameters.)
The code snippet you provided has two flaws that cause problems here.
First, the ...
is missing in the call to MarshalKeyvals
. Without that keyvals
is not expanded into the variadic parameter and instead is passed as a single argument. That is valid because any value, even a slice, can be converted to an interface{}
value. The result is that Keyvals
receives a single element slice with the only element containing your slice of strings. Instead of receiving six strings it receives []interface{}{keyvals}
or, expanding that, it receives []interface{}{[]string{"app", "myapp", "env", "prod", "error", "errormsg"}}
, a slice within a slice, which is not what you intended.
Note that this is the reason that you don't get any output. MarshalKeyvals
is a helper function that is a thin wrapper around Encoder.EncodeKeyvals
, which documents more details about its behavior. The docs for that method are:
EncodeKeyvals writes the logfmt encoding of keyvals to the stream. Keyvals is a variadic sequence of alternating keys and values. Keys of unsupported type are skipped along with their corresponding value. Values of unsupported type or that cause a MarshalerError are replaced by their error but do not cause EncodeKeyvals to return an error. If a non-nil error is returned some key/value pairs may not have be written.
A []string
is an unsupported key type, and thus it is skipped. Since it is the only element of the []interface{}
Go creates to fill in the variadic parameter MarshalKeyvals
returns an empty []byte
.
If we fix that problem by adding the ...
we discover the second problem. keyvals := []string{"app", "myapp", "env", "prod", "error", "errormsg"}
is a slice of strings, and Go will not automatically convert a []string
to a []interface{}
. You can see the compiler error when you run that version on the Go playground. This is a commonly encountered issue and is covered in the Go FAQ entry Can I convert a []T to an []interface{}?
Changing the type of keyvals to a []interface{}
resolves this problem. See this on the playground.
I hope that helps.
from logfmt.
thanks, that's clear! appreciate the guidance!
from logfmt.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logfmt.