Now Reading: “go fmt” command example – The Ranjan.info

Loading
svg
Open

“go fmt” command example – The Ranjan.info

February 6, 20241 min read

The go fmt command in the Go programming language is a tool for formatting Go source code files according to the official Go coding style. It is part of the Go toolchain and serves to maintain a consistent and standardized code layout across all projects. Below is a more detailed description of the go fmt command:

Command Syntax:

# go fmt [package...|file...|directory...]

The primary purpose of Go FMT is to automatically reformat Go source code files to follow Go coding conventions. It enforces a set of style rules to ensure consistency in formatting, such as indentation, spacing, and other code layout considerations.

Output:
When go fmt is executed, it prints the names of the files that were modified during the formatting process. If no changes are made, no output is produced.

“go fmt” command example

1. Format Go source files in the current directory:

2. Format a specific Go package in your import path ($GOPATH/src):

# go fmt /path/to/package

3. Format the package in the current directory and all subdirectories (note…):

4. Print the format in which the commands would have been run, without modifying anything:

5. Print which format commands are being run:

Summary

The go fmt command is an essential tool for Go developers, helping to maintain a unified and readable codebase. By automatically applying formatting rules, it contributes to a standardized and aesthetically pleasing Go code style.

How do you vote?

0 People voted this article. 0 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

Leave a reply

You may like
Loading
svg