-
Notifications
You must be signed in to change notification settings - Fork 34
Format
./godelw format
formats all of the Go files in a project by running gofmt
and ptimports
on them.
-
$GOPATH/src/github.com/nmiyake/echgo
exists and is the working directory - Project contains
godel
andgodelw
- Project contains
main.go
- Project contains
.gitignore
that ignores IDEA files
(Link)
Update the program code to put the echo functionality into a separate package and call it from the main project:
➜ mkdir -p echo
➜ echo 'package echo
func Echo(in string) string {
fmt.Println(strings.Join(os.Args[1:], " "))
}' > echo/echo.go
➜ echo 'package main
import (
"fmt"
"os"
"strings"
"github.com/nmiyake/echgo/echo"
)
func main() {
fmt.Println(echo.Echo(strings.Join(os.Args[1:], " ")))
}' > main.go
Stage these files as a git commit:
➜ git add echo main.go
➜ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: echo/echo.go
modified: main.go
Run ./godelw format
to format all of the files in the project:
➜ ./godelw format
This command formats all of the files using gofmt
and ptimports
. Verify that this command modified the files:
➜ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: echo/echo.go
modified: main.go
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: echo/echo.go
modified: main.go
➜ git diff | cat
diff --git a/echo/echo.go b/echo/echo.go
index f7d4dbe..3e055c2 100644
--- a/echo/echo.go
+++ b/echo/echo.go
@@ -1,4 +1,5 @@
package echo
+
func Echo(in string) string {
fmt.Println(strings.Join(os.Args[1:], " "))
}
diff --git a/main.go b/main.go
index 1d9820b..9087898 100644
--- a/main.go
+++ b/main.go
@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strings"
+
"github.com/nmiyake/echgo/echo"
)
In main.go
, note how the github.com/nmiyake/echgo/echo
import has been separated from the other imports.
ptimports
groups imports into distinct sections consisting of the standard library imports, external imports and
imports that are part of the same package.
Commit the formatted files:
➜ git add main.go echo
➜ git commit -m "Create echo package"
[master 24f63f7] Create echo package
2 files changed, 8 insertions(+), 1 deletion(-)
create mode 100644 echo/echo.go
➜ git status
On branch master
nothing to commit, working directory clean
-
$GOPATH/src/github.com/nmiyake/echgo
exists and is the working directory - Project contains
godel
andgodelw
- Project contains
main.go
- Project contains
.gitignore
that ignores IDEA files - Project contains
echo/echo.go
(Link)
./godelw format
has the following advantages over running gofmt -w ./...
- Only formats files that are part of the project (does not format files in excluded directories)
- Runs
ptimports
in addition togofmt
ptimports
groups Go imports into 3 groups: the Go standard library, packages that are not part of the current project
and packages that are part of the current project.
The output of ./godelw format
is compatible with gofmt
(that is, it is guaranteed that applying gofmt
to a file
formatted by ./godelw format
will be a no-op).
Running ./godelw format
with the --list
(or -l
) flag will output the files that would be changed if
./godelw format
were run without actually applying the changes.
- Home
-
Tutorial
- Add gödel to a project
- Add Git hooks to enforce formatting
- Generate IDE project for GoLand
- Format Go files
- Run static checks on code
- Run tests
- Build
- Run
- Dist
- Publish
- Build and push Docker images
- Generate license headers
- Go generate tasks
- Define excludes
- Write integration tests
- Sync a documentation directory with GitHub wiki
- Verify project
- Set up CI to run tasks
- Update gödel
- Update legacy gödel
- Other commands
- Conclusion
- Name
- Philosophy
- Architecture
- Plugins
- Configuration