Introduction

 Go is no different in having its own syntax or structure and its own flavor like every other language. That’s what makes a language stand out. Go is also easy and elegant in its own way, making it fun to play with. In this article, we will be talking about the following.

  • Go program structure
  • Running a Go program
  • Comments in Go
  • What about semicolons?
  • Revisiting the code

Go Program Structure

 To understand the Go program structure, we need to have a Go program first. So, let’s revisit our favorite “Hello, World!” program. 

  1. package main  
  2.   
  3. import “fmt”  
  4.   
  5. func main() {  
  6.     fmt.Println(“Hello, World!”)  
  7. }  

Every source file (a file with .go extension) has to have package declaration as its first line of code. In our hello-world.go, the first line tells the compiler that this source file is going to contain the entry point for Go, which is the main function. This combination of main package declaration and main function makes it a standalone executable Go program. In our code, we are writing to console which is the standard output. For this to work, we import the fmt package using the import keyword. The fmt package is the short name for format, and it comes with the Go standard library. The fmt package comes with a lot of options for writing to standard output. We will see the syntax later for importing multiple packages. 

Running a Go Program

 We can run any Go program using either “go run” or “go build” command. Using these commands, we instruct the Go compiler to compile and run the Go code. To run our “Hello World” program, we can use the “go run hello-world.go” command, being in the same directory as the program file. 

Go Program Structure

 We can also use the go build which will, in turn, generate an executable named hello-world, that can be run like any other executable. 

Go Program Structure

 If for any reason, we want to change the name of our executable, then we can do so by using the below command. go build -o <file-name> This will create an executable binary with specified <file-name>. For example,

  1. $ go build -o exe //being in the hello-world directory  
  2. $ ./exe // running the executable  
  3. Hello, World!  

As the need may arise, we may want to install our app. To do so, we use thecommand. go install <package-name>  This creates a binary executable and stores it in the $GOPATH/bin directory. So, ensure that the GOBIN is set and added to the PATH environment variable. And since we have the PATH variable set, we can run our binary from anywhere on our system. 

Go Program Structure

Comments

 Adding comments to our code at the right place is as important as writing a good, maintainable code. In Go, comments are not just a way of adding an inline explanation to what your code does, rather it’s also a way of documenting your packages and we will see that when we talk about functions and packages. Well, there are two ways to add comments in your code – line comments or block comments, similar to other programming languages. To add a line comment we simple start with // followed by the comment text. To add a block comment, we wrap our text between the /* and */. Here is an example of both,

  1. // package main defines the entry point  
  2. package main  
  3.   
  4. // import the ‘fmt’ package from standard library  
  5. import “fmt”  
  6.   
  7. /* 
  8.     The main function is the entry point in a Go program. 
  9.     The main function does not have a return type. 
  10.     Also, it does not accept any parameters. 
  11. */  
  12. func main() {  
  13.     fmt.Println(“Hello, World!”)  
  14. }  

What about Semicolons?

 As you must have observed in the above code, we have not put a single semicolon. Because behind the scenes, Go does that for us. Like C, Go’s grammar also uses a semicolon to terminate statements, but they don’t have to appear in the source code. Here is what the Go’s documentation says about it, Like C, Go’s formal grammar uses semicolons to terminate statements, but unlike in C, those semicolons do not appear in the source. Instead, the lexer uses a simple rule to insert semicolons automatically as it scans, so the input text is mostly free of them.The rule is this. If the last token before a newline is an identifier (which includes words like int and float64), a basic literal such as a number or string constant, or one of the tokens: break continue fallthrough return ++ — ) }, the lexer always inserts a semicolon after the token. This could be summarized as, “if the newline comes after a token that could end a statement, insert a semicolon”. A semicolon can also be omitted immediately before a closing brace, so a statement such as tokens break continue fallthrough return ++ — ) } needs no semicolons. To read more about the internals, go ahead and spend some time with Go Docs

Revisiting the Code

 As we learned earlier, every standalone application must have a package main declaration, and a .gofile with such declaration must have a main function. This main function is the entry point for our application, as in other programming languages like C, Java, C# etc. The difference is, that in Java or C# the main function can accept arguments (string[] args), while in Go the main function does not accept any arguments. Also, the main function does not return anything. Notice how we are importing the fmt package: import “fmt”. This syntax works only when we are importing a single package. If we want to import multiple packages, then we need to group our packages in import (). Here is an example,

  1. >// importing multiple packages in Go  
  2. import (  
  3.     “context”  
  4.     “database/sql”  
  5.     “fmt”  
  6.     “log”  
  7. )  

Note that we have not put a ; at the end of any imported package, since this will be done for us by the lexer. 

Conclusion

 Go has a very simple program structure, which feels and reads more like C. Following the same pattern, we have the main function as the entry point for our application. Go offers a convenient way of writing clean code by smartly inserting semicolons where required. We have different types of comments for logical descriptions in our code. We will later see how these comments build the documentation for our packages. To better understand what happens behind the scenes and how we can write quality code in Go, I would highly recommend you to check out the Effective Go page from Go docs. Related Articles

BÌNH LUẬN

Please enter your comment!
Please enter your name here