Skip to content

Instantly share code, notes, and snippets.

@christian-korneck
Created October 1, 2022 19:08
Show Gist options
  • Save christian-korneck/dd1d48811e730b576b0ecf3e5ab0f328 to your computer and use it in GitHub Desktop.
Save christian-korneck/dd1d48811e730b576b0ecf3e5ab0f328 to your computer and use it in GitHub Desktop.

overwrite a function (or just anything). Target doesn't need to be exported. Go doesn't support overloading. Can't be done multiple times (i.e. trying to overwrite time.Sleep() fails as it's actually the unexported runtime.timeSleep()).

package main

import (
	"fmt"
	_ "unsafe" // required to enable go:linkname

	"log"
)

//go:linkname boom log.Print
func boom(v ...any) {
	_ = v
	fmt.Println("boom")

}

func main() {
	log.Print("hi") // prints "boom", not "hi"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment