A Quine in Go


tl;dr Quines are cool. Create one and share yours!

Before I slept last night, I read a post on /r/golang that invited people to post their own version of the famous fizzbuzz program. Many of them put mine to shame, there were people did some fancy things.

Instead of creating a version of fizzbuzz in Go with my own flare, I recalled a past homework assignment where I had to create a Quine in Go. From Wikipedia,

"A quine is a computer program which takes no input and produces a copy of its own source code as its only output."

Here's my one liner (explanation included later):

package main;import"fmt";func main(){s:=`package main;import"fmt";func main(){s:=%c%s%c;fmt.Printf(s,96,s,96)}`;fmt.Printf(s,96,s,96)}

Although its pretty basic and adopted from a sample C version, I enjoyed writing it. Even more giddy to see the creative things that other people can come up with, and created a Github Repo for people to contribute and share their Quines in Go, Ruby, C, whatever.

Create your own

Helpful resources :

-Quines By David Madore, A very thorough resource with examples and explanations.

-The Quine Page By Gary P. Thompson II, Quines of varying implementations

Explanation of my Quine

(skip for final thoughts)

package main;import"fmt";func main(){s:=`package main;import"fmt";func main(){s:=%c%s%c;fmt.Printf(s,96,s,96)}`;fmt.Printf(s,96,s,96)}

I wanted to make it as compact and simple as possible, hence the semicolons (I'm sorry fellow Gophers) so that I could place every statement on one line.

Notice that the main function only has two statements, a declaration of a string literal s, and a call to the Printf function of the fmt package.

The magic is in the second statement, fmt.Printf(s,96,s,96). As usual, the first argument to Printf is a string that contains format specifiers (%c, %s, %c, respectively). The next three arguments satisfy values that the format specifiers require. 96 is the ASCII decimal equivalent of the backtick character.

Now that we have that out of the way, let's step through the output made by Printf, substituting one format specifier at a time:

The String n

package main;import"fmt";func main(){s:=%c%s%c;fmt.Printf(s,96,s,96)}

Substituting the first format specifier %c

package main;import"fmt";func main(){s:=`%s%c;fmt.Printf(s,96,s,96)}    

Substituting the second format specifier %s

package main;import"fmt";func main(){s:=`package main;import"fmt";func main(){s:=%c%s%c;fmt.Printf(s,96,s,96)}%c;fmt.Printf(s,96,s,96)}

Substituting the last format specifier %c

package main;import"fmt";func main(){s:=`package main;import"fmt";func main(){s:=%c%s%c;fmt.Printf(s,96,s,96)}`;fmt.Printf(s,96,s,96)}

And that's about it! Again, there are Quines out there that make this implementation seem like child's play. I recommend viewing those for drawing the greatest inspiration.

Final Thoughts and Comments

  • I originally wanted to make a repository strictly of quines in Go, but did not want to limit contributors to the repo. I instead opted to create branches by language.

  • Feedback is always appreciated. Also, if you've found any resources that you'd like to share, let me know!

  • A concurrent Quine would be epic, huh?