Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Run the "go fmt" tool on your source code files, and it will fix indentation, fix spacing issues, align comments, and more, automatically.
-
go fmt
- Fixes indentation
- Fixes spacing issues
- Aligns comments
- And more
Before:
package main
import ("fmt")
func main() {
var myInt int
var myString string
var myBool bool
fmt.Println("myInt", myInt) // 0
fmt.Println("myString", myString) //""
fmt.Println("myBool",myBool) //false
}
After:
package main
import (
"fmt"
)
func main() {
var myInt int
var myString string
var myBool bool
fmt.Println("myInt", myInt) // 0
fmt.Println("myString", myString) //""
fmt.Println("myBool", myBool) //false
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
There's one last thing we should cover
before we move onto the next stage, and
0:00
that's the Go-Format Tool.
0:03
Here we have a file whose
formatting isn't ideal.
0:05
Things aren't indented correctly, there's
odd spacing in the function calls and
0:08
things like that.
0:13
Let's try running the Go-Format
tool on this file.
0:15
So, I'm gonna go down here to the console.
0:18
I'll type go, I'll run the format
sub-command which is just fmt for short.
0:19
And I'll specify the name of
the file that I want it to update.
0:26
So that's temp.go.
0:29
Run that and it'll print out the name
of the file that it updated.
0:31
Then if I switch back up here
to the editor, it will reload.
0:35
And you can see that even though
the code looks much the same,
0:38
it's been substantially reformatted.
0:41
Our import statement's been cleaned up.
0:43
The indentation on
everything has been fixed.
0:45
The comments are all
aligned with each other and
0:48
even the spacing inside
each line has been fixed.
0:51
This was done for
us automatically by the Go Format tool.
0:54
It analyzed the structure
of our source code and
0:57
then indented everything appropriately for
us, and it makes other fixes as well.
0:59
Most people don't like all the formatting
changes that Go Format makes.
1:04
But the point is that you don't
have to argue about those changes.
1:08
Go Format is the standard for
formatting Go code.
1:11
The standard format is easier
to read once you're used to it.
1:14
And you don't have to manually
make changes anymore to match some
1:17
arbitrary style guide.
1:21
You should run Go Format on all your Go
code, especially if you're sharing it.
1:22
And it's easy to do, sublime text, then
and all the other major text editors can
1:27
be set up to automatically run Go Format
on files each time you save them.
1:31
So get in the habit, Go Format can
save you a lot of time and trouble
1:36
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up