Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2012 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
io.Reader foo.Reader | ||
`raw string test` 'c' 10i 10 10.0 | ||
|
||
package goplay | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"go/ast" | ||
"go/parser" | ||
"go/printer" | ||
"go/token" | ||
"net/http" | ||
) | ||
|
||
type fmtResponse struct { | ||
Body string | ||
Error string | ||
} | ||
|
||
func fmtHandler(w http.ResponseWriter, r *http.Request) { | ||
resp := new(fmtResponse) | ||
for a < b { | ||
a *= 2 | ||
} | ||
body, err := gofmt(r.FormValue("body")) | ||
if err != nil { | ||
resp.Error = err.Error() | ||
} else { | ||
resp.Body = body | ||
} | ||
json.NewEncoder(w).Encode(resp) | ||
} | ||
|
||
func gofmt(body string) (string, error) { | ||
fset := token.NewFileSet() | ||
f, err := parser.ParseFile(fset, "prog.go", body, parser.ParseComments) | ||
if err != nil { | ||
return "", err | ||
} | ||
ast.SortImports(fset, f) | ||
var buf bytes.Buffer | ||
err = printer.Fprint(&buf, fset, f) | ||
if err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} |