UNIX/Go语言之父 Ken Thompson 在1983年的图灵奖演讲 Reflections on Trusting Trust 就给出了一个C语言的自重写程序。
Copy main(a){printf(a="main(a){printf(a=%c%s%c,34,a,34);}",34,a,34);}
Copy /* Go quine */
package main
import "fmt"
func main () {
fmt. Printf ( " %s%c%s%c \n" , q, 0x 60 , q, 0x 60 )
}
var q = `/* Go quine */
package main
import "fmt"
func main() {
fmt.Printf(" %s%c%s%c \n", q, 0x60, q, 0x60)
}
var q = `
Copy package main ; func main (){c := "package main;func main(){c:= %q ;print(c,c)}" ; print (c,c)}
Copy package main ; func main (){ print (c + "\x60" + c + "\x60" )}; var c = `package main;func main(){print(c+"\x60"+c+"\x60")};var c=`
Copy func If (condition bool , trueVal, falseVal interface {}) interface {} {
if condition {
return trueVal
}
return falseVal
}
a, b := 2 , 3
max := If ( a > b , a , b ).( int )
println ( max )
Copy func main () {
defer func () { for {} }()
}
func main () {
defer func () { select {} }()
}
func main () {
defer func () { <- make ( chan bool ) }()
}
Copy func main () {
for i := range random ( 100 ) {
fmt. Println (i)
}
}
func random (n int ) <-chan int {
c := make ( chan int )
go func () {
defer close (c)
for i := 0 ; i < n; i ++ {
select {
case c <- 0 :
case c <- 1 :
}
}
}()
return c
}
Copy type testing_TBHelper interface {
Helper ()
}
func Assert (tb testing . TB , condition bool , args ...interface {}) {
if x, ok := tb.( testing_TBHelper ); ok {
x. Helper () // Go1.9+
}
if ! condition {
if msg := fmt. Sprint (args ... ); msg != "" {
tb. Fatalf ( "Assert failed, %s " , msg)
} else {
tb. Fatalf ( "Assert failed" )
}
}
}
func Assertf (tb testing . TB , condition bool , format string , a ...interface {}) {
if x, ok := tb.( testing_TBHelper ); ok {
x. Helper () // Go1.9+
}
if ! condition {
if msg := fmt. Sprintf (format, a ... ); msg != "" {
tb. Fatalf ( "Assertf failed, %s " , msg)
} else {
tb. Fatalf ( "Assertf failed" )
}
}
}
func AssertFunc (tb testing . TB , fn func () error ) {
if x, ok := tb.( testing_TBHelper ); ok {
x. Helper () // Go1.9+
}
if err := fn (); err != nil {
tb. Fatalf ( "AssertFunc failed, %v " , err)
}
}