LEARN GOLANG| Part-2

  • Concept of Arrays in GoLang.
  • Slices in GoLang.
  • Maps in GoLang.
  • Making Map an ordered Map.
  • Functions in GoLang.
  • Functions that takes-in input unknown number of values.
  • Functions that returns multiple values.
  • Call by Value in GoLang.
  • Call by Reference in GoLang.
  • Usage of Pointers for plain values in GoLang.
  • Usage of Pointers for Objects in GoLang.
  • Demonstrate an example of Objects with constructors.
  • Coding to Interfaces.
  • We have declared an Array of Strings at line #8. This is an Array of 4 Strings have been initialised.
  • At Line #9, the array of 5 integer values have been explicitly initialised and in subsequent lines, we are assigning values to each array index. Note that, we are just using the “=” symbol i.e. assignment operator, because we don’t need to infer the type anymore.
  • Also, we have demonstrated the usage of “len” method, to showcase the length of the array here. Recall that, this method “len” comes from a special package named builtin. The Go compiler assumes that the builtin package is always imported.
  • Note that, at line #14, we are trying to add more entries tot he Array earlier declared. And we can add infinite elements, there is no constraint of size here.
  • Also observe that, at line #16, we are performing the sorting operation of the Array Slice.
  • At line #26, We are iterating over the elements in the slice, you can use the conditional for loop. We start with zero, up to the length of the loons, and then i++, print the loon.
  • At line #32, as demonstrated, we also have a range keyword.
  • At line #38, If you want both the index and the value, we can use range with a double value context on the left side. So here in line 38 I have both i and name, and then I can print that the name is at a certain position.
  • If you want just the values, we have to use the underscore, as in line 43, because unused variables in Go are a compilation error.
  • Note that, we are using make() function at line #8, in order to initialise the Map.
  • At line #8, we have defined the Map with both <K,V> as of string type.
  • At line #16, we are also performing the delete operation basis of key.
  • Functions in GO can very well return multiple values too.
  • Functions receive variables and also have return-types defined in definition itself :-
  • This method can receive infinite values in input.
  • This method would return ONE value of int type.
  • Here In Go-Lang, we can attach functions to your custom types. They’re then referred to as methods.
  • In a more completely Object Oriented language such as Java, you’d say that each method is a member of a class whereas In Go, a method is a member of a type.
  • So what do we think is going to happen? Will the dog bark three times, or nine times? And the answer is that it only barks three times.
  • That’s because, when you pass the dog object in as the receiver, a copy is made of it, it’s not a reference. If you want it to be a reference, you can use pointers. But within the function, this is a brand new copy of the object, so when I modify the sound field, I’m not modifying the version that was created in the main function.
  • Call by ValueWhen Go passes an integer to a function, Go passes it by value which means, Go will create a copy of this integer and pass it to the function. Any changes to the integer inside the function won’t affect the original value.
  • Call by ReferenceHowever, when Go passes a slice or a map to a function, it passes it by reference. This means that inside the function body, you’re working with the exact same object that was passed and any changes to the slice you make inside the function stay after the function is over.
  • Demo #1 Here, *int indicates that, thisVar is a pointer and not value. And if I don’t assign anything, that means that, variable will be nil; it doesn’t contain anything.
  • Demo #2 Here, we are printing the *thisVar, which indicates that, thisVar is a pointer and not value. This time, it throws the RuntimeError indicating the InvalidMemoryAddress.
  • Here, variable “thisParticularInVar” is valued at 75.
  • The Pointer-Variable “thisPointerVar” is pointing to the address of variable thisParticularInVar.
  • The notation “&thisParticularInVar” is pointing to the address of variable thisParticularInVar.
  • The notation “*thisPointerVar” is referring to the actual value being stored at address stored in thisPointerVar.
  • Notice that, at line #8, we specifically want the type of the variable to be inferred. Here, we want that GO-compiler should be auto-inferring the type of 75.
  • Note that, At line #14, we modified the value of the variable ‘thisParticularInVar’ through it’s pointer variable.
  • The change made through the pointer variable is very well reflecting in the original variable too.
  • At line #21, the variable “p” houses the address of an object (which is of type “Point”).
  • At line #15, the address of object, is being received by the variable “p”.
  • At line #22, also note that, “Move()” is a method which is being invoked through a reference variable. Here, the methods are not combined together with the struct class.

--

--

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