First Quine (program that prints itself) in GO

441 views
Skip to first unread message

ashani

unread,
Jan 26, 2011, 7:11:07 AM1/26/11
to golang-nuts
Just as all languages have a documented quine (program that prints
itself), GO should also have his...This is the first quine written in
GO..Hope we will have more quines in GO..The output of the following
code snippet is the code snippet itself !! and in a formatted way..

package main

import (
"fmt"
)

func main() {
prelude := "package main\n\nimport (\n\t\"fmt\"\n)\n\nfunc main() {\n
\tprelude := "
format := "%s%cpackage main%cn%cnimport (%cn%ct%c%cfmt%c%c%cn)%cn
%cnfunc main() {%cn%ctprelude := %c%c%cformat := %c%s%c%c
%cfmt.Printf(format, prelude, 34, 92, 92, 92, 92, 92, 34, 92, 34, 92,
92, 92, 92, 92, 34, 10, 9, 34, format, 34, 10, 9, 10, 10);%c}%c"
fmt.Printf(format, prelude, 34, 92, 92, 92, 92, 92, 34, 92, 34, 92,
92, 92, 92, 92, 34, 10, 9, 34, format, 34, 10, 9, 10, 10)
}



roger peppe

unread,
Jan 26, 2011, 7:28:00 AM1/26/11
to ashani, golang-nuts
i hate to burst your bubble, but Russ posted one some
ago here.

http://research.swtch.com/2010/03/zip-files-all-way-down.html

reproduced here:

/* Go quine */
package main
import "fmt"
func main() {
fmt.Printf("%s%c%s%c\n", q, 0x60, q, 0x60)
}
var q = `/* Go quine */
package main
import "fmt"
func main() {
fmt.Printf("%s%c%s%c\n", q, 0x60, q, 0x60)
}
var q = `

ashani

unread,
Jan 26, 2011, 7:37:32 AM1/26/11
to golang-nuts
Russ has a very elegant one !
Reply all
Reply to author
Forward
0 new messages