Discussion:
How to write a Go-program that prints its own source code? (Quine)
Ondekoza
2011-01-24 08:57:46 UTC
Permalink
Of course I could perhaps replicate the C-example from

http://en.wikipedia.org/wiki/Quine_(computing)

but the question is: Does Go permit a smarter approach?

Keep up the _go_od work!

Stefan S.
fango
2011-01-24 10:00:34 UTC
Permalink
http://research.swtch.com/2010/03/zip-files-all-way-down.html
Ondekoza
2011-01-24 11:15:47 UTC
Permalink
OK, so, it can be done. Thank you, fango.

Now, can it be done without _duplicating_ the source code in a string?
Because having the same code twice would not guarantee that the
printed version is identical to the run version, not even counting the
maintenance nightmare.
Post by fango
http://research.swtch.com/2010/03/zip-files-all-way-down.html
roger peppe
2011-01-24 12:57:12 UTC
Permalink
Post by Ondekoza
OK, so, it can be done. Thank you, fango.
Now, can it be done without _duplicating_ the source code in a string?
Because having the same code twice would not guarantee that the
printed version is identical to the run version, not even counting the
maintenance nightmare.
just run it twice, and check that the output is the same as the input.
then use the output from the first run as the code.
Russ Cox
2011-01-25 17:17:09 UTC
Permalink
Post by Ondekoza
Now, can it be done without _duplicating_ the source code in a string?
Because having the same code twice would not guarantee that the
printed version is identical to the run version, not even counting the
maintenance nightmare.
package main

import (
"io"
"os"
)

func main() {
f, _ := os.Open("x.go", 0, 0)
io.Copy(os.Stdout, f)
}
HaWe
2011-01-25 19:43:06 UTC
Permalink
Thanks for a healthy laught.
Post by Russ Cox
Post by Ondekoza
Now, can it be done without _duplicating_ the source code in a string?
Because having the same code twice would not guarantee that the
printed version is identical to the run version, not even counting the
maintenance nightmare.
package main
import (
        "io"
        "os"
)
func main() {
        f, _ := os.Open("x.go", 0, 0)
        io.Copy(os.Stdout, f)
}
Ondekoza
2011-01-26 09:32:26 UTC
Permalink
Well, this kind of answer was to be expected, sooner or later. And I
guess that Russ is aware of the fact that this is kinda cheating.

But let me explain my initial request, because my question has a deep
motivation that is perhaps not obvious. Some companies have trouble
respecting GPL-style licenses (I _know_ that Go is _not_GPL-style,
that's not the point). If the developer of an application wants to
ensure that the source-code of his/her application is bundled with the
applicaiton in the first place, there is no (ok, less) need to worry
about the correct delivery of this component to third parties in the
end probably violating the license.

If there were a Go-package, that would allow me to re-create the
source-code
from the binary, than I'd definitely use it for GPL-style software!


Stefan 'ondekoza' Schroeder
Post by Russ Cox
Post by Ondekoza
Now, can it be done without _duplicating_ the source code in a string?
Because having the same code twice would not guarantee that the
printed version is identical to the run version, not even counting the
maintenance nightmare.
package main
import (
        "io"
        "os"
)
func main() {
        f, _ := os.Open("x.go", 0, 0)
        io.Copy(os.Stdout, f)
}- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
ashani
2011-01-26 07:06:16 UTC
Permalink
Here is one that produces formatted output identical to that formated
by gofmt:

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)
}
mattn
2011-01-26 09:47:25 UTC
Permalink
BTW, Is there plan to add __FILE__ into go?
Anyone knows?
roger peppe
2011-01-26 10:19:44 UTC
Permalink
func __FILE__() (f string) {
_, f, _, _ = runtime.Caller(1)
return
}

func Caller(skip int) (pc uintptr, file string, line int, ok bool)
Post by mattn
BTW, Is there plan to add __FILE__ into go?
Anyone knows?
Jessta
2011-01-26 10:23:58 UTC
Permalink
Post by mattn
BTW, Is there plan to add __FILE__ into go?
Anyone knows?
http://golang.org/pkg/runtime/#Caller
Will give you the file name.

- jessta
--
=====================
http://jessta.id.au
Loading...