go-tutorial

Go Basic Syntax

Let’s see a simple example of Go programming language.

package main
import "fmt" 
func main() { 
 fmt.Println("Hello, World")
}

Output:

Hello, World

  • The first line is the package deceleration, and the package name is main. Package deceleration is required for all go programmes.
  • The following line is an import statement; in this case, we’re importing “fmt.” The files from the package fmt will be included by the compiler.
  • The following line contains a main() function; all execution begins with this function.
  • The following line, fmt.Println(…), is a Go function. The message “Hello, World” will be printed on the screen by this function.

How to run Go program:

We have saved the program as Hello.go. To run the program, open the command prompt and write:

go run Hello.go

Output:

Hello, World

Go Compact Code

You can write more compact code, like shown below

package main; import ("fmt"); func main() { fmt.Println("Hello World!");}

Note : This is not recommended because it makes the code more difficult to read.

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published.