Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
ece0d0e7
Commit
ece0d0e7
authored
Mar 09, 2012
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go/printer: example for Fprint
R=golang-dev, dsymonds, rsc CC=golang-dev
https://golang.org/cl/5785057
parent
29199aa4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
0 deletions
+67
-0
src/pkg/go/printer/example_test.go
src/pkg/go/printer/example_test.go
+67
-0
No files found.
src/pkg/go/printer/example_test.go
0 → 100644
View file @
ece0d0e7
// 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.
package
printer_test
import
(
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"strings"
"testing"
)
// Dummy test function so that godoc does not use the entire file as example.
func
Test
(
*
testing
.
T
)
{}
func
parseFunc
(
filename
,
functionname
string
)
(
fun
*
ast
.
FuncDecl
,
fset
*
token
.
FileSet
)
{
fset
=
token
.
NewFileSet
()
if
file
,
err
:=
parser
.
ParseFile
(
fset
,
filename
,
nil
,
0
);
err
==
nil
{
for
_
,
d
:=
range
file
.
Decls
{
if
f
,
ok
:=
d
.
(
*
ast
.
FuncDecl
);
ok
&&
f
.
Name
.
Name
==
functionname
{
fun
=
f
return
}
}
}
panic
(
"function not found"
)
}
func
ExampleFprint
()
{
// Parse source file and extract the AST without comments for
// this function, with position information referring to the
// file set fset.
funcAST
,
fset
:=
parseFunc
(
"example_test.go"
,
"ExampleFprint"
)
// Print the function body into buffer buf.
// The file set is provided to the printer so that it knows
// about the original source formatting and can add additional
// line breaks where they were present in the source.
var
buf
bytes
.
Buffer
printer
.
Fprint
(
&
buf
,
fset
,
funcAST
.
Body
)
// Remove braces {} enclosing the function body, unindent,
// and trim leading and trailing white space.
s
:=
buf
.
String
()
s
=
s
[
1
:
len
(
s
)
-
1
]
s
=
strings
.
TrimSpace
(
strings
.
Replace
(
s
,
"
\n\t
"
,
"
\n
"
,
-
1
))
// Print the cleaned-up body text to stdout.
fmt
.
Println
(
s
)
// output:
// funcAST, fset := parseFunc("example_test.go", "ExampleFprint")
//
// var buf bytes.Buffer
// printer.Fprint(&buf, fset, funcAST.Body)
//
// s := buf.String()
// s = s[1 : len(s)-1]
// s = strings.TrimSpace(strings.Replace(s, "\n\t", "\n", -1))
//
// fmt.Println(s)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment