<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>oops on Golang</title><link>https://golang.k5kc.com/categories/oops/</link><description>Recent content in oops on Golang</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 16 Aug 2020 00:12:00 +0530</lastBuildDate><atom:link href="https://golang.k5kc.com/categories/oops/index.xml" rel="self" type="application/rss+xml"/><item><title>Adding behaviour to any type using Type Aliasing</title><link>https://golang.k5kc.com/2020/08/16/7.adding-behaviour-to-client-types-using-type-aliasing/</link><pubDate>Sun, 16 Aug 2020 00:12:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/7.adding-behaviour-to-client-types-using-type-aliasing/</guid><description>To define methods on a type you don’t “own”, you need to define an alias for the type you want to extend:
package main import ( &amp;#34;fmt&amp;#34; &amp;#34;strings&amp;#34; ) type MyStr string func (s MyStr) Uppercase() string { return strings.ToUpper(string(s)) } func main() { fmt.Println(MyStr(&amp;#34;test&amp;#34;).Uppercase()) } http://www.golangbootcamp.com/book/methods</description></item><item><title>Attaching Method to any type</title><link>https://golang.k5kc.com/2020/08/16/attaching-method-to-any-type/</link><pubDate>Sun, 16 Aug 2020 00:12:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/attaching-method-to-any-type/</guid><description> We can attach methods to any type in Go. For e.g.
// Basic Types int string float64 // Bare Types array struct // ----------------------- // Do not use &amp;#34;Pointer Receivers&amp;#34; with below types since they already carry a pointer with themselves. // i.e. slice, map, chan, func // ----------------------- // Pointer Bearing Types slice map chan // Channels // We can also attach methods to: func</description></item><item><title>Code Organization</title><link>https://golang.k5kc.com/2020/08/16/6.code-organization/</link><pubDate>Sun, 16 Aug 2020 00:12:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/6.code-organization/</guid><description>Methods can be defined on any file in the package, but my recommendation is to organize the code as shown below:
Define constants Define vars Define interface Define struct Implementation of methods on struct Example:
package models // list of packages to import import ( &amp;#34;fmt&amp;#34; ) // list of constants const ( ConstExample = &amp;#34;const before vars&amp;#34; ) // list of variables var ( ExportedVar = 42 nonExportedVar = &amp;#34;so say we all&amp;#34; ) type Fly interface { Fly() string } // Main type(s) for the file, // try to keep the lowest amount of structs per file when possible.</description></item><item><title>Implement Sort Interface</title><link>https://golang.k5kc.com/2020/08/16/sort/</link><pubDate>Sun, 16 Aug 2020 00:12:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/sort/</guid><description>sort package defines an interface whose name is Interface:
type Interface interface { // Len is the number of elements in the collection. Len() int // Less reports whether the element with // index i should sort before the element with index j. Less(i, j int) bool // Swap swaps the elements with indexes i and j. Swap(i, j int) } For slice, or any other collection types, provided that it implements the Len(), Less and Swap functions, you can use sort.</description></item><item><title>Inheritance and Composition in GO</title><link>https://golang.k5kc.com/2020/08/16/3.inheritance-and-composition/</link><pubDate>Sun, 16 Aug 2020 00:12:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/3.inheritance-and-composition/</guid><description>Structs can &amp;ldquo;inherit&amp;rdquo; from other structs by a method known as embeding. Here is a simple example:
package main import ( &amp;#34;fmt&amp;#34; ) type a struct { Name string } //embeds a value of type a type b struct { a } //embeds a pointer to an a type c struct { *a } func main() { a := a{Name: &amp;#34;Janeway&amp;#34;} fmt.Println(a.Name) b := &amp;amp;b{a: a} fmt.Println(b.Name) c := &amp;amp;c{a: &amp;amp;a} fmt.</description></item><item><title>Method receiver vs Pointer receiver</title><link>https://golang.k5kc.com/2020/08/16/5.method-receiver-vs-pointer-receiver/</link><pubDate>Sun, 16 Aug 2020 00:12:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/5.method-receiver-vs-pointer-receiver/</guid><description>Struct values when used as method receivers are exactly that values and so are a shallow copy of the value allocated a new portion of memory. The effects are not observed outside of the method as there are no references to the new value and so it is garbaged collected.
Pointer receivers allow mutation of what the pointer points to. Your function is recieving a pointer to the same address in memory even in the function stackframe.</description></item><item><title>Methods in GO</title><link>https://golang.k5kc.com/2020/08/16/4.methods/</link><pubDate>Sun, 16 Aug 2020 00:12:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/4.methods/</guid><description>Intro We have already seen golang functions. Now the question is:
what is the difference between a function and a method in Go?
Answer is -
A method is a function that has a defined receiver, in OOP terms, a method is a function on an instance of an object.
Go doesnt have classes but structs, and we can add receiver methods on structs to add behaviour.
To add some behaviour to a struct you can have it be a method reciever.</description></item><item><title>Interface in Go</title><link>https://golang.k5kc.com/2020/08/16/2.interface/</link><pubDate>Sun, 16 Aug 2020 00:11:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/2.interface/</guid><description>Intro We declare an Interface much like as we define a user defined type. Interfaces decouple different types from each other so we can create more maintainable programs.
More:
An Interface is a Protocol, a Contract.
Bigger the Interface the weaker the abstraction. &amp;ndash;&amp;gt; Rob Pike
It&amp;rsquo;s an abstract type. It doesn&amp;rsquo;t have any implementation. It only describes the expected behavior.
The opposite of Abstract Type is Concrete Type.
All the types in Go except Interface are of Concrete Type.</description></item></channel></rss>