Tuesday 23 May 2023

"Hello, World!" program in Go

"Hello, World!" program in Go(Golang) : Lets write our first program in Go, and try to understand menaing of each line.

go
package 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:

go
go run hello.go

The output will be:

Hello, World!

This "Hello, World!" program is a simple example to demonstrate the basic structure of a Go program. It showcases how to import packages, define functions, and use the fmt package to print output to the console. You can build upon this foundation to create more complex and feature-rich Go programs.

No comments:

Post a Comment

Seven front-end development trends in 2023-2024

With the increasing prevalence of apps in the digital landscape , the role of front-end developers remains essential. While apps aim to ove...

Popular Posts