Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
Certainly! Let's dive into creating a simple "Hello, World!" program using Go.
Before writing your first Go program, ensure you have Go installed. If not, you can download and install it from the official Go website.
Create a new file: Use your favorite text editor or IDE to create a new file named hello.go
.
Type in the following code:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
Let's break down the code:
package main
: Every Go program starts with a package declaration. The main
package is the starting point for a runnable Go program.
import "fmt"
: This includes the fmt
package which contains functions for formatted I/O.
func main()
: This is the main function. Execution of a Go program starts and ends here.
fmt.Println("Hello, World!")
: This line uses the Println
function from the fmt
package to print "Hello, World!" to the console.
Open the terminal or command prompt.
Navigate to the directory containing your hello.go
file.
Compile and run the program using the following command:
go run hello.go
Hello, World!
printed on the console.Alternatively, you can compile the program to an executable using go build hello.go
and then run the generated executable directly.
Congratulations! You've just written and executed your first Go program.