Go is a statically-typed, concurrent, and garbage-collected language that is designed for building fast and reliable applications. In this tutorial, we’ll start by writing a simple “Hello, World!” program in Go.
Here’s the code for a “Hello, World!” program in Go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Let’s walk through the code line by line:
- The first line,
package main
, specifies the package that this code belongs to. In Go, a package is a collection of related Go files. Themain
package is a special package that contains the entry point for the program, which is themain
function. - The next line,
import "fmt"
, imports thefmt
package, which provides basic I/O functions, such asPrintln
. - The
main
function is the entry point for the program and is called when you run the program. In this case, themain
function calls thePrintln
function from thefmt
package to print the string “Hello, World!” to the console.
To run the program, you can save the code in a file named main.go
and run the following command in the terminal:
go run main.go
This will compile and run the program, and you should see the output “Hello, World!” displayed in the terminal.
Congratulations, you’ve written your first Go program! From here, you can explore other features of the language and start building more complex programs.
Leave a Reply