How to print Hello World! In Golang?

Go ProgrammingServer Side ProgrammingProgramming

In this tutorial, we are going to explore the code for printing the Hello World! In Golang. Also, we will see by using which package and function we can do this thing.

To print anything in Golang we import the fmt package that includes the various functions to print anything in Golang.

Different function fmt package consists of.

  • Print()

  • Printf()

  • Println()

Syntax

packageName.functionName(“Thing you want to print”)

Algorithm

  • Step 1 − Import the fmt package after package declaration.

  • Step 2 − Go to the respective function where you want to print.

  • Step 3 − Call any function from the package with the package name and dot operator and pass Hello World!.

Example

In this example we are going to pring Hello world using all three print function i.e. Print(), Printf(),Println().

package main // fmt package provides the function to print anything import ( "fmt" ) // this is the function that will get executed by the // compiler first func main() { /* In this program, we are going to print the Hello World! using the Golang language. As to printing anything on the terminal, we need somefunction to do that like printf() in C language. In Golang also we have a Printf() function and the package That includes the fmt that we have imported at the top. */ // here we have passed the string Hello World! that we get // print on the console // \n is used to break the line in the end fmt.Printf("Hello World!\n") //printing using PrintLn function fmt.Println("Hello World!") //Printing using normal Print function fmt.Print("Hello World!\n") }

Output

Hello World!
Hello World!
Hello World!

Conclusion

In this way, we can print Hello World! in Golang by using the respective package and function. To learn more about go you can explore these tutorials.

raja
Updated on 30-Sep-2022 08:01:41

Advertisements