<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>method on Golang</title><link>https://golang.k5kc.com/tags/method/</link><description>Recent content in method 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/tags/method/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>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>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></channel></rss>