Functions

Declaration

func <name>(arg <type>) <return type> {
//body
return <val>
}
func firstFn(arg1 int, arg2 int) int {
val := arg1 + arg2
fmt.Println("I am a method and my value is : ", val)
return val
}

Calling a function


Pointers

Initialize a pointer

arrA := [...] int{1, 3, 5}
var b *int = &arrA[0]

Modifying the pointer referenced value

arrA := [...] int{1, 3, 5} var b *int = &arrA[0] fmt.Println("value…


Map

Method 1
variable := map[<key type>]<value type> {comma separated key,value}
emptyMap := map[string]int{}
fmt.Println(emptyMap)
Result
map[]
ORarrMap := map[[3]string]int{}OR statePop := map[string]int { "UP": 235, "Maharashtra": 200, "Delhi": 150, "MP": 175, "Rajasthan": 165, "Karnataka": 150…


Arrays

Method 1
<variable> := [size of array] <type> {valueset}
This will initialize an array of size as specified in - <size of array>
grades := [3] int{97, 98, 99}
Method 2
<variable> := [...] <type> {valueset}
This will initialize an array with size as in the number of elements in the valueset.
grades := [...] int{97,98, 99, 100}
size - 4
Method 3 var <variable> [size of array]…


DataTypes

var checkIfTrue bool = true
fmt.Printf("%v, %T\n", checkIfTrue, checkIfTrue)
var postId int = 10
commentId := 50
f := 3.14 fmt.Printf("%v, %T\n", f, f) f…


Declaration

1. var <name> <type> Example → var i int
2. var <name> = <value> Example → var i = 10
3. <name> := <value> Example → i := “hey”
  • 1 and 2 can be categorised as var <name> <=/type>. …


Let's consider the project structure as :
* bin
* pkg
* src
* github.com
* prateekgupta3991
* helloworld
* main.go
NOTE — Current working directory — learnGo
go run Example - go run src/github.com/anyjoe/helloworld/main.gogo build ./Example - go build src/github.com/anyjoe/helloworld
./helloworld
go install bin/Example - go install src/github.com/anyjoe/helloworld
bin/helloworld

Prateek Gupta

Senior Software Developer from India. Fancy learning and building stuff. Making a habit to share knowledge via blogs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store