gopackage main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Explanation:
1. `package main`: This line defines the package name. In Go, the main package is special and is used to create an executable program. It should always be declared as package main for a standalone executable program. If you compare this with Java this is like class which having main() method.
2. `import "fmt"`: This line imports the fmt package, which provides various functions for formatted I/O operations, including printing to the console. We need to import the fmt package to use the Println() function later in the code.
3. `func main()`: This line defines the main() function. It is the entry point of the program, and execution starts from here.
4. `fmt.Println("Hello, World!")`: This line calls the Println() function from the fmt package to print the string "Hello, World!" to the console. The Println() function adds a newline character after printing the output.
Lets try to run this program now, to run this program, save it with a .go extension (e.g., hello.go) and use the go run command in your terminal:
gogo run hello.go
Hello, World!
No comments:
Post a Comment