HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
A Go file consists of the following parts
A Go file consists of the following parts:
Look at the following code, to understand it better:
package main
import ("fmt")
func main() {
fmt.Println("Hello World!")
}
Example explained
package
keyword. In this example, the program belongs to the
main
package.
import ("fmt")
lets us import files included in
the fmt
package.
func main() {}
is a function. Any code inside its curly brackets {}
will be executed.
fmt.Println()
is a function made available from the fmt
package. It is used to output/print text. In our example it will output "Hello World!".
Note: In Go, any executable code belongs to the
main
package.
fmt.Println("Hello World!")
is a statement.
In Go, statements are separated by ending a line (hitting the Enter key) or by
a semicolon ";
".
Hitting the Enter key adds ";
" to the end of the line implicitly (does not show up in the source code).
The left curly bracket {
cannot come at the start of a line.
Run the following code and see what happens:
package main
import ("fmt")
func main()
{
fmt.Println("Hello World!")
}
You can write more compact code, like shown below (this is not recommended because it makes the code more difficult to read):
package main; import ("fmt"); func main() { fmt.Println("Hello World!");}