Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Go

You're reading from   Mastering Go Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures

Arrow left icon
Product type Paperback
Published in Aug 2019
Publisher Packt
ISBN-13 9781838559335
Length 798 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mihalis Tsoukalos Mihalis Tsoukalos
Author Profile Icon Mihalis Tsoukalos
Mihalis Tsoukalos
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

1. Go and the Operating System 2. Understanding Go Internals FREE CHAPTER 3. Working with Basic Go Data Types 4. The Uses of Composite Types 5. How to Enhance Go Code with Data Structures 6. What You Might Not Know About Go Packages and Functions 7. Reflection and Interfaces for All Seasons 8. Telling a UNIX System What to Do 9. Concurrency in Go – Goroutines, Channels, and Pipelines 10. Concurrency in Go – Advanced Topics 11. Code Testing, Optimization, and Profiling 12. The Foundations of Network Programming in Go 13. Network Programming – Building Your Own Servers and Clients 14. Machine Learning in Go 15. Other Books You May Enjoy

Two Go rules

Go has strict coding rules that are there to help you avoid silly errors and bugs in your code, as well as to make your code easier to read for the Go community. This section will present two such Go rules that you need to know.

As mentioned earlier, please remember that the Go compiler is there to help and not make your life miserable. As a result, the main purpose of the Go compiler is to compile and increase the quality of your Go code.

You either use a Go package or you do not include it

Go 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 naive program, which is saved as packageNotUsed.go:

package main 
 
import ( 
    "fmt" 
    "os" 
) 
 
func main() { 
    fmt.Println("Hello there!") 
} 
In this book, you are going to see lots of error messages, error situations, and warnings. I believe that looking at code that fails to compile is also useful and sometimes even more valuable than just looking at Go code that compiles without any errors. The Go compiler usually displays useful error messages and warnings that will most likely help you to resolve an erroneous situation, so do not underestimate Go error messages and warnings.

If you execute packageNotUsed.go, you will get the following error message from Go and the program will not get executed:

$ go run packageNotUsed.go
# command-line-arguments
./packageNotUsed.go:5:2: imported and not used: "os"
  

If you remove the os package from the import list of the program, packageNotUsed.go will compile just fine; try it on your own.

Although this is not the perfect time to start talking about breaking Go rules, there is a way to bypass this restriction. This is showcased in the following Go code that is saved in the packageNotUsedUnderscore.go file:

package main 
 
import ( 
    "fmt" 
    _ "os" 
) 
 
func main() { 
    fmt.Println("Hello there!") 
} 

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:

$ go run packageNotUsedUnderscore.go
Hello there!
  
The reason that Go is allowing you to bypass this rule will become more evident in Chapter 6, What You Might Not Know About Go Packages and Go Functions.

There is only one way to format curly braces

Look at the following Go program named curly.go:

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

Although 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:

$ go run curly.go
# command-line-arguments
./curly.go:7:6: missing function body for "main"
./curly.go:8:1: syntax error: unexpected semicolon or newline before {
  

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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image