Instagram
youtube
Facebook
Twitter

Syntax

Formatting

  • In Golang formatting, the code is much easier as the machine takes care of most of the formatting issues.
  • The gofmt package in Golang takes care of formatting and emits the code in standard style and in the vertical format.

Golang Formatting rules

  • Indentation - Golang uses tabs for indentation and gofmt by default takes care of it.
  • Line Length - Golang has no upper limit for line length. If you feel the line getting too long just use an extra tab to indent it.
  • Parentheses - Golang only uses parentheses with if, for, and switch. It is much lesser than other programming languages like C, C++, and Java.

 

Golang Comments

Golang allows to comment the code in two ways

  • C-Style /* */ Block comments.
  • C++ style // line comments.
Example of Block Comment

package main

import "fmt"

/*
func main() {
	fmt.Println("Hello World") 
}

*/

 

Example of Line Comment

package main

import "fmt"

func main() {
	fmt.Println("Hello World")  //Line Comment
}

 

Semicolons

  • Golang works in the same way as C works. Go by default uses semicolons to terminate statements.
  • But we don't have to use semicolons while writing the code. Golang's lexer automatically inserts semicolons as it scans the code.
  • Golang follows the rule to insert semicolons automatically if the last token before a new line is an identifier (like int and float64).