Quick actions

cmd+k|ctrl+k

Navigation

Languages

Essential Go / Interfaces

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:01:20Z

Updated

2019-03-26T10:01:20Z

package main

import (
	"fmt"
	"strconv"
)

// Stringer is an interface with a single method
type Stringer interface {
	String() string
}

// User struct that implements Stringer interface
type User struct {
	Name string
}

func (u *User) String() string {
	return u.Name
}

// Any type can implement an interface. Here we create
// an alias of int type an implement Stringer interface

type MyInt int

func (mi MyInt) String() string {
	return strconv.Itoa(int(mi))
}

// printTypeAndString accepts an interface. 's' can be any value
// that implements Stringer interface
func printTypeAndString(s Stringer) {
	fmt.Printf("%T: %s\n", s, s)
}

func main() {
	u := &User{Name: "John"}
	printTypeAndString(u)

	n := MyInt(5)
	printTypeAndString(n)
}
INFO