Important Rules of Go

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 Gol…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by kuldeep_singh

Image description

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.

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

You can read full article on this site
programmingeeksclub.com

Thanks for reading :)


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by kuldeep_singh


Print Share Comment Cite Upload Translate Updates
APA

kuldeep_singh | Sciencx (2022-10-07T14:01:30+00:00) Important Rules of Go. Retrieved from https://www.scien.cx/2022/10/07/important-rules-of-go/

MLA
" » Important Rules of Go." kuldeep_singh | Sciencx - Friday October 7, 2022, https://www.scien.cx/2022/10/07/important-rules-of-go/
HARVARD
kuldeep_singh | Sciencx Friday October 7, 2022 » Important Rules of Go., viewed ,<https://www.scien.cx/2022/10/07/important-rules-of-go/>
VANCOUVER
kuldeep_singh | Sciencx - » Important Rules of Go. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/07/important-rules-of-go/
CHICAGO
" » Important Rules of Go." kuldeep_singh | Sciencx - Accessed . https://www.scien.cx/2022/10/07/important-rules-of-go/
IEEE
" » Important Rules of Go." kuldeep_singh | Sciencx [Online]. Available: https://www.scien.cx/2022/10/07/important-rules-of-go/. [Accessed: ]
rf:citation
» Important Rules of Go | kuldeep_singh | Sciencx | https://www.scien.cx/2022/10/07/important-rules-of-go/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.