avatarSajal Dulal

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2438

Abstract

or-class">.printMarks</span>() shyam<span class="hljs-selector-class">.printMarks</span>() }</pre></div><div id="674f"><pre><span class="hljs-keyword">func</span> <span class="hljs-params">(m grade11Marks)</span> <span class="hljs-title function_">printMarks</span><span class="hljs-params">()</span> { fmt.Println(<span class="hljs-string">"math: "</span>, m.math, <span class="hljs-string">" physics: "</span>, m.physics) }</pre></div><div id="294a"><pre><span class="hljs-keyword">func</span> <span class="hljs-params">(g grade12Marks)</span> <span class="hljs-title function_">printMarks</span><span class="hljs-params">()</span> { fmt.Println(<span class="hljs-string">"math: "</span>, g.math, <span class="hljs-string">" physics: "</span>, g.computer) }</pre></div><p id="3214">Go ahead and run the code with the command <i>go run main.go</i>. Basically, we declare two different types of struct <b>grade11Marks</b> and <b>grade12Marks</b> and with one variable for each <b>ram</b> and <b>shyam</b> respectively. If you do not have knowledge regarding structs in GO please refer to the previous article that cover it.</p><p id="28d1">We create a receiver function named <b>printMarks()</b> for each struct separately which are used to print the values inside the struct variable.</p><p id="8d44">Now, these are two structs are very similar, and we could have a lot of common functions for these two to work together. Like, lets say storing in database, or even printing the values.</p><p id="ae2e">But one of the major restrictions in GO, is that it is a strictly typed language and we cannot pass two different <b>type</b> variables to a <b>function</b>, even if the action performed inside the <b>function</b> is very similar to both the <b>functions</b>.</p><p id="17b3">Now, below is an example, where we can use an interface to represent both the types of struct <b>grade11Marks</b> and <b>grade12Marks</b> and pass both the values and perform actions on them. However, the example below might seem very trivial, but in bigger projects where there are a lot more functions and variables, this could be a very useful method for code re-usability.</p><p id="8200">Go ahead add the following code before the <b>struct</b> definitions:</p><div id="28ed"><pre><span class="hljs-keyword">type</span> <span class="hljs-type">marks </span><span class="hljs-keyword">interface</span> { printMarks() }</pre></div><p id="e44a">This is

Options

a definition of an <b>interface</b> type named <b>marks</b>. What this does is basically tell GO , that all the variables that have a <b>receiver function</b> which is named as <b>printMarks</b>, then they all belong to the type marks. So basically <b>interface</b> is a type in GO that incorporates multiple types. If you do not understand concept behind <b>receiver function</b> please read the previous article regarding such.</p><p id="2e0d">Note: if the <b>receiver function</b> returns some value, then it is to be mentioned. Like, if our <b>function</b> had a return type of string then instead of just <b>printMarks()</b> we needed to write <b>printMarks()</b> string.</p><p id="5825">Now, add the following <b>function</b>, at the end of the code:</p><div id="4abe"><pre>func <span class="hljs-built_in">print</span>(<span class="hljs-selector-tag">b</span> <span class="hljs-attribute">marks</span>) { <span class="hljs-selector-tag">b</span><span class="hljs-selector-class">.printMarks</span>() }</pre></div><p id="6a29">What, this <b>function</b> does is, takes a value b of type marks, which is an interface that incorporates all the types that have the receiver function <b>printMarks</b>. And, then we call the <b>printMarks()</b> function to perform this action. There could be more complex situation here, that could save us a lot of code redundancy. But for the sake of simplicity we are simply calling the <b>printMarks()</b> receiver function.</p><p id="a266">Now, to actually use this function, remove the following lines:</p><div id="da77"><pre> ram<span class="hljs-selector-class">.printMarks</span>() shyam<span class="hljs-selector-class">.printMarks</span>()</pre></div><p id="4483">And, add the following lines:</p><div id="9d1e"><pre> <span class="hljs-function"><span class="hljs-title">print</span>(<span class="hljs-variable">ram</span>)</span> <span class="hljs-function"><span class="hljs-title">print</span>(<span class="hljs-variable">shyam</span>)</span></pre></div><p id="34ba">Now, run the program and see the output.The same output as before is seen.</p><p id="b50f">Once again, interfaces are initially hard to grasp, but is actually a fundamental concept in GO for better code usability. I hope this gives a basic understanding of <b>Interfaces </b>in GO. If there are any questions feel free to leave a response. Stay tuned for more articles regarding GO. Happy Coding :)</p></article></body>

Interfaces in GOlang

This is the Fifth article in the “Getting Started with GOlang” series. If you have not checked out the previous article please check it out before diving into this. 1. Getting started with GOlang 2. Slices, Custom Types and Receiver Functions(methods) in GOlang 3. Structs and Maps in GOlang 4. Pointers and Passby value/reference in GOlang 5. Interfaces in GOlang

INTERFACES

Before learning anything about what interfaces are, lets take a look at an example to clearly get the idea regarding why we need interfaces and how it could save us a lot of trouble while coding with GO.

Create a new directory and inside it create a main.go file. Paste the following code inside it.

package main
import "fmt"
type grade11Marks struct {
 math    int
 physics int
}
type grade12Marks struct {
 math     int
 computer int
}
func main() {
 ram := grade11Marks{50, 80}
 shyam := grade12Marks{60, 70}
 ram.printMarks()
 shyam.printMarks()
}
func (m grade11Marks) printMarks() {
 fmt.Println("math: ", m.math, " physics: ", m.physics)
}
func (g grade12Marks) printMarks() {
 fmt.Println("math: ", g.math, " physics: ", g.computer)
}

Go ahead and run the code with the command go run main.go. Basically, we declare two different types of struct grade11Marks and grade12Marks and with one variable for each ram and shyam respectively. If you do not have knowledge regarding structs in GO please refer to the previous article that cover it.

We create a receiver function named printMarks() for each struct separately which are used to print the values inside the struct variable.

Now, these are two structs are very similar, and we could have a lot of common functions for these two to work together. Like, lets say storing in database, or even printing the values.

But one of the major restrictions in GO, is that it is a strictly typed language and we cannot pass two different type variables to a function, even if the action performed inside the function is very similar to both the functions.

Now, below is an example, where we can use an interface to represent both the types of struct grade11Marks and grade12Marks and pass both the values and perform actions on them. However, the example below might seem very trivial, but in bigger projects where there are a lot more functions and variables, this could be a very useful method for code re-usability.

Go ahead add the following code before the struct definitions:

type marks interface {
 printMarks()
}

This is a definition of an interface type named marks. What this does is basically tell GO , that all the variables that have a receiver function which is named as printMarks, then they all belong to the type marks. So basically interface is a type in GO that incorporates multiple types. If you do not understand concept behind receiver function please read the previous article regarding such.

Note: if the receiver function returns some value, then it is to be mentioned. Like, if our function had a return type of string then instead of just printMarks() we needed to write printMarks() string.

Now, add the following function, at the end of the code:

func print(b marks) {
 b.printMarks()
}

What, this function does is, takes a value b of type marks, which is an interface that incorporates all the types that have the receiver function printMarks. And, then we call the printMarks() function to perform this action. There could be more complex situation here, that could save us a lot of code redundancy. But for the sake of simplicity we are simply calling the printMarks() receiver function.

Now, to actually use this function, remove the following lines:

 ram.printMarks()
 shyam.printMarks()

And, add the following lines:

 print(ram)
 print(shyam)

Now, run the program and see the output.The same output as before is seen.

Once again, interfaces are initially hard to grasp, but is actually a fundamental concept in GO for better code usability. I hope this gives a basic understanding of Interfaces in GO. If there are any questions feel free to leave a response. Stay tuned for more articles regarding GO. Happy Coding :)

Interfaces
Golang
Tutorial
Development
Beginner
Recommended from ReadMedium