What is gofmt and gofmt-w-l ?

What is gofmt?'s picture

As we all know there are various inbuilt packages available in all the programming languages to make it easy for the programmers to work on the programs and build their own projects.

What is a package in the programming languages?

A package is a namespace that sorts out a bunch of related classes and connection points. Thoughtfully you can consider packages being like various organizers on your computer system. These packages contain countless classes and functions helpful to go developers for various prerequisites. There are two types of packages inbuilt and user-defined.

  • Inbuilt packages

Inbuilt packages are the ones already available with the programming languages. And all the user needs to include them or import them to use all the predefined functions and benefits.

  • User-defined packages

As the name indicates these packages are characterized by the user. Users create a directory name whose name ought to be equivalent to the name of the package. Then users create a class inside the directory.

What is gofmt?

fmt stands for Format. It is an inbuilt package of Go used for formatting basic strings to values to any other datatype. It prints them and also takes the user’s input like scanf on cin does in C and C++ respectively. If you’re a programmer well-versed with C and C++ it is something like header files <stdio> in C and <iostream> in C++. The only difference is it implements formatted input/ouput.

The formatted “active words” are derived from C’s but obviously much simpler and easy to understand. fmt format your program’s source code in a neat and natural way. It is highly recommended to use it before you submit your source code for public viewing or committing a version on Github. fmt can be executed on the command line or you can configure your IDE (Integrated Development Environment) for Go like Visual Studio Code or Go Sublime.


1 2 3 4 5 6 7 package main import "fmt" func main() { fmt.Println("Hello, Bonjour") }

In the above example, the fmt package uses the function “Println” to print a certain line. I have saved the program as hello.go which will be easy to locate as the program is all about printing hello.

The output will be -

Integrated Development Environment

Another example -

1 2 3 4 5 6 7 8 9 10 package main import "fmt" func main() { var a int // variable declaration fmt.Scanln(&a) fmt.Println("Enter an int value: ") fmt.Println(a) }

Output -

IDE (Integrated Development Environment)
You should also read about Top 6 Golang Backend Framework For Developer

What is gofmt-w-l?

gofmt-w-l is a command used from the format package.

gofmt-w individually writes the result back to the source to file. It actually overwrites the original file as per the Go language’s guidelines.

gofmt-w-l name_of_the_directory where your .go file is stored this command tells you which file has been modified in the directory.

gofmt-w-l this command uses the name of the directory.

Syntax -

1 gofmt-w-l name_of_the_direcotry

For example to run the command in the command line -

$ cd //to change the current directory to the parent directory

$ls //to list the files

1 $gofmt-w-l demo_structure/demo_proj/first_go_prog.go

//shows the file name which has been modified which is first_go_prog.go in this example

Wrap up

This article is to help you understand the basics of the fmt package in Go and give a few introductory examples of how to run the few commands. Happy programming!!

Build Your Golang Team