Hello World in Golang
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
In this program:
- We start with the package declaration
package main
, which indicates that this is the main package and serves as the entry point of the program. - We import the required package
fmt
, which stands for “format,” and it provides functions for formatted input and output. - In the
main
function, we usefmt.Println()
to print the string “Hello, World!” to the console.
To run this Go program, follow these steps:
- Save the code in a file named
hello.go
(or any other name with the.go
extension). - Open a terminal or command prompt.
- Navigate to the directory where you saved the
hello.go
file. - Run the following command:
go run hello.go
If everything is set up correctly, you will see the output:
Hello, World!
That’s it! You’ve just written and executed your first Go program. Congratulations!