Say Hello world in Go lang
Writing your first program "Hello World"
package main
import "fmt"
func main( ) {
fmt.Print("Hello World")
}
Understanding the program line by line
- As we have discussed in the overview of go lang that Go program is organized in the various package. Here first statement package main specifies that this program will belong to the package main.
- Next line import "fmt" includes the predefined package fmt in your program so that you can use the functions (features) of this package in your program. Here import is the keyword in go lang use to import package(s).
- fmt stands for the format, fmt package has the various pre-defined function like Print, Println, Printf to print the formatted text and other methods like Scanf for reading the formatted values.
- Next Line defines the main() function, which is the starting point of every program. func is a keyword in go lang to define a function.
- Within the main() function we have written fmt.Print("Hello World"), which prints "Hello World" on the output screen.
- Observe the "P" in Println(), It is written in Capital. This is a feature called "Exporting" in go language. Which means a Variable/ function is accessible outside the package if its name starts with Caps.
- In Go lang, a new line automatically ends the statement so fmt.Print() don't ends with semicolon(;) like in other programming languages C / C++ / Java.
Terminology used
- A package can be understood like a folder/directory in which you can manage multiple files, similarly, in go language, various related files are grouped together and put into a package for better management.
- In general, a function is anything which performs something or we can say a function is the group of statements together to perform a specific task.
- Multiple statements grouped together by using a block created by curly block{}.
- Print and Println both are pre-defined functions used to print the output but Println automatically changes the line after printing the enclosed output. So that the next output starts printing from the new line.
Comments