Show Menu

Cheat sheet for Go Programming language syntax.


Packages in Go supports modula­rity, encaps­ula­tion, separate compil­ation, and reuse.
Package declar­­ation at top of every source file
Standalone execut­­ables program are in package main
If an entity is declared within a function, it is local to that function.
If declared outside of a function, however, it is visible in all files of the package to which it belongs
The case of the first letter of a name determines its visibility across package bounda­ries.
 ­  Upper case identi­­fier: Exported i.e visible and acces­­sible outside of its own package.
 ­  Lower case identi­­fier: private (not accessible from other packages)


var x int = 11
*int is intege­rPo­inter type.
‘p’ will contain the address of an integer variable.
You can also say that p points to an int variable.
var p *int


var a[3] int  ­ ­ ­ ­ ­ ­ // Array of 3 integers
a[len(­a)-1]  ­ ­ ­ ­ ­ ­  // Print last element

Naming Convention

// a name begins with a letter or an underscore and may have any number of additional letters, digits, and unders­cores
type pl­aye­­r­­Score struct // Use CamelCase
const MaxT­ime int
var file­C­­losed bool // Use the complete words in larger scopes
var arg []string // Use fewer letters in smaller scopes
var local­API string // Use All caps for acronym


There are four major kinds of declar­ations: var, const, type, func
var name type = expression
// Either the type or the =expre­ssion part may be omitted, but not both
// If the type is omitted, it is determined by the initia­lizer expres­sion.
If the expression is omitted, the initial value is the zero value for the type, which is 0 for numbers, false for booleans.
var s, sep string    ­ ­        // implicit initialize
var foo, bar int = 42, 13 // declare and init. var name type = expression
s, sep := "­", "­"  ­ ­          // Short variable declar­ation. name := expression
A constant is an identifier for a fixed value. The value of a variable can vary, but the value of a constant must remain constant.
const constant = "This is a consta­nt"
const a float64 = 3.14
Function Declar­ation
A function declar­ation has a name, a list of parame­ters, an optional list of results
// function with params
func getFul­lNa­me(­fir­stName string, lastName string) {}
// Multiple params of the same type
func getFul­lNa­me(­fir­stName, lastName string) {}
// Can return type declar­ation
func getId() int
// Can return multiple values at once
func person() (int, string) {
  return 23, "­vin­ay"
// Can return multiple named results
func person() (age int, name string) {
  age = 23 name = "­vin­ay"
var age, name = person()
// Can return function
func person() func() (strin­g,s­tring) {
  area:=­func() (strin­g,s­tring) {
   ­  return "­str­eet­", "­cit­y"
return area


// There only for, no while, no until
for i := 1; i < len(os.Args); i++ {}   // initia­liz­ation; condition; post {}
for condition {}  ­ ­ ­ ­  // While loop
// 'range' produces a pair of values: the index and the value of the element at that index. '_' is called blank identi­fier.
for _, arg := range os.Arg­s[1:]

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          GoLang fmt Printing Cheat Sheet
          Golang Naming Conventions Cheat Sheet
          GoLang Cheat Sheet