Share

Important Rules of Golang

Home » Programming Language » Golang » Important Rules of Golang
important-rules-of-golang

Golang has strict coding rules that are there to help developers avoid silly errors and bugs in your golang code, as well as to make your code easier for other to read(for the Golang community).
This article will cover two such Important Rules of Golang you need to know of it.

Use of Golang package

Golang has strict rules about package usage. Therefore, you cannot just include any package you might think that you will need and then not use it afterward.
Look at the following program and you’ll understand after you try to execute it.

// First Important Rules of Golang
// never import any package which is
// not going to be used in the program
package main

import (
    "fmt"
    "log" // imported and not used log error
)

func main() {
    fmt.Println("Package log not used but is included in this program")
}

If you execute your program(whatever name of the file), you will get the following error message from Golang and the program will not get execute.

Output:

~/important-rules-of-go$ go run main.go
# command-line-arguments
./main.go:8:2: imported and not used: "log"

If you remove the log package from the import list of the program, it will compile just fine without any errors.
Let’s try to run program after removing log package from program.

// valid go program
package main

import (
    "fmt"
)

func main() {
    fmt.Println("Package log not used but is included in this program")
}

If you execute this code you will not get any error on compile time.

Output:

~/important-rules-of-go$ go run main.go
Package log not used but is included in this program

Although this is not the perfect time to start talking about breaking Golang rules, there is a way to bypass this restriction. This is showcased in the following code

// alternative way to bypass this 
// imported and not used rule
// just via adding underscore
// before the name of the package
// if you are not going to use that
// package
package main

import (
    "fmt"
    _ "log"
)

func main() {
    fmt.Println("Package log not used but is included in this program")
}

So, using an underscore character in front of a package name in the import list will not create an error message in the compilation process even if that package will not be used in the program
If you execute the above code you’ll see no error as we got in before this example:

Output:

~/important-rules-of-go$ go run main.go
Package log not used but is included in this program

Only one way to format curly braces

Look at the following Golang program:

package main

import (
    "fmt"
)

func main()
{
    fmt.Println("Golang has strict rules for curly braces!")
}

Well it looks just fine? If you try to execute it, you will be fairly disappointed, because you will get the following syntax error message and the code will not compile and therefore run so if you have started learning Golang after C or C++ then you should be very careful with curly braces, As we know Golang have similar syntax like C that’s true but not that so much.

Output:

~/important-rules-of-go$ go run main.go
# command-line-arguments
./main.go:8:6: missing function body
./main.go:9:1: syntax error: unexpected semicolon or newline before {

The official explanation of this error message is that Go requires the use of semicolons as statement terminators in many contexts, and the compiler automatically puts (inserts) the required semicolons when it thinks that they are necessary. Therefore, putting the opening curly braces ( { ) in its own line will make the Go compiler put(insert) a semicolon at the end of the pervious line ( func main() ), which is the cause of the error message(termination of the program).

Now let’s see what is the right way to use curly braces in Golang.

// right way to use curly braces in golang
package main

import (
    "fmt"
)

func main() {
    fmt.Println("Golang has strict rules for curly braces!")
}

Now execute this code and you will see, there is no syntax error message.

Output:

~/important-rules-of-go$ go run main.go
Golang has strict rules for curly braces!

Conclusion

In this article we have covered what are the most important rules of Golang, which every developer should have idea if he/she is learning about Golang and how they can avoid all those problems, rules we covered in this article is Use of Golang package, which is never import the package you are not going to use in program or if you imported the package then you can also bypass it using underscore to program not fail on compile time and last rule we explained is, Only one way to format curly braces, there is only single way to use curly braces which we explained in this article.

Thanks for reading 🙂

close

Don't miss the
Awesome Articles

We don’t spam!