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 themain
package, which is necessary for creating executable programs.import "fmt"
: This line imports thefmt
package from the Go standard library. Thefmt
package provides functions for formatted input and output, includingPrintln
.func main() { ... }
: This defines themain
function, which serves as the entry point for the program.fmt.Println("Hello, World!")
: This line calls thePrintln
function from thefmt
package 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.go
is 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