Certainly! Here's the classic "Hello, World!" program in Go:
gopackage mainimport "fmt"func main() {fmt.Println("Hello, World!")}
Explanation:
package main: This line declares that this Go file is part of themainpackage, which is necessary for creating executable programs.import "fmt": This line imports thefmtpackage from the Go standard library. Thefmtpackage provides functions for formatted input and output, includingPrintln.func main() { ... }: This defines themainfunction, which serves as the entry point for the program.fmt.Println("Hello, World!"): This line calls thePrintlnfunction from thefmtpackage to print the string "Hello, World!" to the console.
To run this program:
- Save the code in a file named
hello.go. - Open a terminal or command prompt.
- Navigate to the directory where
hello.gois located. - Run the program using the command
go run hello.go. - You should see the output
Hello, World!printed on the console
No comments:
Post a Comment