dark corners of go

gopher

number literals

1_000_000
0_600
0x_42_af_3d

prevent unkeyed literals

type X struct {
A, B int
_ struct{}
}

x := X{A: 1, B: 2} // OK
x := X{1, 2} // Error: too few values
type X1 struct {
A, B int
_ struct{}
}

type X2 struct {
_ struct{}
A, B int
}

var (
x1 *X1
x2 *X2
)

fmt.Printf(
"sizeof(X1) = %d sizeof(X2) = %d\n",
unsafe.Sizeof(*x1), unsafe.Sizeof(*x2)
) // sizeof(X1) = 24 sizeof(X2) = 16

type grouping

type (
T1 struct{}
T2 struct{}
)

pass multiple return value to another function

func add2(x, y int) (int, int) {
return x+2, y+2
}
func add4(x, y int) (int, int) {
return x+4, y+4
}
func main() {
// OK x = 7, y = 8
x, y := add4(add2(1, 2))
// ERROR multiple-value add2() in single-value context
fmt.Printf("x = %d y = %d", add2(1, 2))
}

interface composition

type I1 interface {
X() int
}

type I2 interface {
Y()
}

type I3 interface {
I1
I2
Z() int
}
type I1 interface {
X() int
}

type I2 interface {
X()
}

type I3 interface {
I1
I2 // ERROR: duplicate method 'X'
}

typed iota

const (
_ = iota
testvar // will be int
)
type myType int
const (
_ myType = iota
testvar // will be myType
)

type alias vs type definition

type SameMutex = sync.Mutex
type NewMutex sync.Mutex
m1 := SameMutex{}
m2 := NewMutex{}
m1.Lock() // OK
m2.Lock() // ERROR: Unresolved reference 'Lock'

package init()

import _ "net/http/pprof"
package mainvar X intfunc init() {
X = 1
}
var x2 = 2 * Xfunc main() {
println(x2)
}

special packages and directories

go list ./... | grep -v /vendor/ | xargs -L1 golint -set_exit_status
os.Open("testdata/data.json")

type hint

type A struct {
X string
Y string
}

func Decode(useThisType interface{}, binaryData []byte) interface{} {
json.Unmarshal(binaryData, useThisType)
return useThisType
}

func main() {
value := Decode(&A{}, []byte(`{"X":"10", "Y":"20"}`))
fmt.Println(value)
}

detailed output with %v

type A struct {
X string
Y string
}

func main() {
fmt.Printf("%v", A{X: "10", Y:"20"})
fmt.Printf("%+v", A{X: "10", Y:"20"})
fmt.Printf("%#v", A{X: "10", Y:"20"})
}
{10 20}
{X:10 Y:20}
main.A{X:"10", Y:"20"}

Example functions

func Example() {...}
func ExampleT() {...}
func ExampleExamples_output() {
fmt.Println("Hello")
// Output: Hello
}

error wrapping

e1 := errors.New("error one")
e2 := fmt.Errorf("e2 : %w", e1)
fmt.Println(e2)
e2 : error one
e3 := errors.Unwrap(e2)
fmt.Println(e3)
error one

embed files

package main

import _ "embed"

func main() {
//go:embed "hello.txt"
var s string
print(s)
}
Hello, Gophers!

forcing a type to json marshal

type Data struct {
ID int64 `json:"id,string"`
}

block forever

select {
}

comment magic

// Object is an object
type Object struct {}
// +build linux
// +build linux windows
// +build linux,386
// #include <stdio.h>
//
// static void myprint(char *s) {
// printf("%s\n", s)
// }
import "C"
C.myprint(C.String("foo"))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store