Show Menu
Cheatography

Cheat sheet for Go Programming language syntax.

Packages

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)

Pointers

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
// Expression &var (address of var) yields a pointer to a variable.
p = &x // will contain address of x
// Expression *p points to the variable whose address p contains. *p is an alias for x.
fmt.Pr­int­ln(*p)

Arrays

Arrays and Structs are aggregate type
Arrays are homoge­neous
Array is fixed length sequence of zero or more elements of particular type.
var a[3] int  ­ ­ ­ ­ ­ ­ // Array of 3 integers
var a[3]int = [3]int{1, 2, 3} // use an array literal to initialize an array with a list of values
a[len(­a)-1]  ­ ­ ­ ­ ­ ­  // Print last element
q := [...]i­nt{1, 2, 3} // with ellipsis ... , array length is determined by the number of initia­lizer
 

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

Declar­ations

There are four major kinds of declar­ations: var, const, type, func
var
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 foo int = 42 // declare and init. var name type = expression
var sep string    ­ ­        // implicit initialize
s, sep := "­", "­"  ­ ­          // Short variable declar­ation. name := expression
p := new(int) // p, of type *int, points to an unnamed int variable
// new(T) creates unnamed variable of type T, initialize it to the zero value of T and returns its address.
const
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"
  return
}
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
}

Loops

// 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:]
 

Comments

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 Cheat Sheet
          Golang Naming Conventions Cheat Sheet