Skip to content

Instantly share code, notes, and snippets.

@blanchonvincent
blanchonvincent / empty_interface_test.go
Last active August 9, 2019 18:09
Medium - Empty interface
package main_test
import (
"testing"
)
var x MultipleFieldStructure
type MultipleFieldStructure struct {
a int
@blanchonvincent
blanchonvincent / empty_interface.go
Created August 9, 2019 12:42
Medium - Empty interface
func main() {
var i int8 = 1
read(i)
}
//go:noinline
func read(i interface{}) {
println(i)
}
@blanchonvincent
blanchonvincent / empty_interface.go
Last active August 9, 2019 12:43
Medium - Empty interface
func main() {
var i int8 = 1
read(i)
}
//go:noinline
func read(i interface{}) {
n := i.(int16)
println(n)
}
@blanchonvincent
blanchonvincent / logrus.go
Created August 6, 2019 12:10
Medium - zap package
log.SetOutput(os.Stdout)
log.WithFields(log.Fields{
"url": "http://foo.com",
"attempt": 3,
"backoff": time.Second,
}).Info("failed to fetch URL")
@blanchonvincent
blanchonvincent / zerolog.go
Created August 4, 2019 15:32
Medium - Zap package
l := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr})
l.Info().
Str("url", `http://foo.com`).
Int("attempt", 3).
Dur("backoff", time.Second).
Msg("failed to fetch URL")
@blanchonvincent
blanchonvincent / logger.go
Last active August 4, 2019 14:42
Medium - Zap package
logger.Info("failed to fetch URL",
// Structured context as strongly typed Field values.
zap.String("url", `http://foo.com`),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second),
)
@blanchonvincent
blanchonvincent / cli.go
Last active July 31, 2019 17:36
Medium - Cobra
package main
import (
"log"
"os"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
package main
import (
"github.com/spf13/cobra"
)
func main() {
cmd := newCommand()
cmd.AddCommand(newNestedCommand())
package main
import (
"context"
"flag"
"github.com/google/subcommands"
)
type GreetCommand struct {}
func (g *GreetCommand) Name() string { return "greet" }
@blanchonvincent
blanchonvincent / base.go
Created July 31, 2019 06:19
Medium - Cobra
type Command struct {
// Run runs the command.
// The args are the arguments after the command name.
Run func(cmd *Command, args []string)
// UsageLine is the one-line usage message.
// The first word in the line is taken to be the command name.
UsageLine string
// Short is the short description shown in the 'go help' output.