What is gofmt and gofmt-w-l ?

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.
Example-
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 -

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 -

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!!

Mansi Gaur is a freelancing blogs and e-books writer who keeps you up with the trending technologies and user guides. A blogger who is currently a post-graduate living in United Kingdom and trying to make her niche as a Data Scientist. Before taking a deep dive into the "Data-World", she got a Bachelor's Technology degree in Computer Science and has always dreamed of writing as a kid which inspired her to write wonderful content with the right amount of technical terms to make it easy for the beginners and as well full-fledged developers to grasp a hold onto the computer technologies.