Go:git gofmt Pre-commit Hook

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

We use Git to do version control of Go source, before we commit, we need to automatically format the code, and when unformatted, output some information, here is a script like this:

Note: This script originates from: Http://golang.org/misc/git/pre-commit
About the small article about cleaning Go code: An Introduction to Go Tools and clean Code

  #!/bin/sh # Copyright: the Go Authors.        All rights reserved.        # Use of this source code was governed by a Bsd-style # license that can being found in the license file.        # git gofmt pre-commit hook # to use, store as. Git/hooks/pre-commit inside your repository and make sure        # It has execute permissions.        # # This script does not handle file names, that contain spaces.        gofiles=$ (git diff--cached--name-only--diff-filter=acm | grep '. go$ ') [-Z ' $gofiles] && exit 0 unformatted=$ (Gofmt-l $gofiles) [-Z ' $unformatted] && exit 0 # Some files is not gofmt ' d.        Print message and fail. echo >&2 "Go files must is formatted with GOFMT. Please run: "for FN in $unformatted; Do echo >&2 "gofmt-w $PWD/$fn" done exit 1  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.