58 lines
2.3 KiB
Markdown
58 lines
2.3 KiB
Markdown
|
---
|
||
|
title: "Learning Go: Methods and Pointer Receivers"
|
||
|
date: 2017-12-29T10:46:08-05:00
|
||
|
draft: true
|
||
|
tags: ["golang", "learning"]
|
||
|
---
|
||
|
|
||
|
# I gotta get Go-ing with this language
|
||
|
|
||
|
Being in the position I am, I've worked with almost nothing but exclusively interpreted languages, namely Ruby and Python.
|
||
|
Both of these are great tools to have to solve problems and to solve them quickly. It is easy to pick up an idea and get
|
||
|
a prototype going in Python or Ruby due to not having to run any compilation, code changes can be tested instantly often by
|
||
|
just rerunning the interpreter. However, this can be a problem when programs get large. I've been a part of projects
|
||
|
where there are hundreds of thousands of lines of Ruby and Python code where changes to some backend call can cause a ten second
|
||
|
difference in the load time of the frontend client.
|
||
|
|
||
|
![Image](/img/post/golang.gif)
|
||
|
|
||
|
|
||
|
People much smarter than me at Google have created and actively develop the programming language Go, which is a compiled language
|
||
|
that does a lot of cool things out of the box. Knowing how many problems I've had in development that could be potentially solved by Go
|
||
|
(and missing compilers which catch some nasty bugs before the software starts), I want to learn Go and blog about cool things I find or cool
|
||
|
things I do with it.
|
||
|
|
||
|
## Methods in Golang?
|
||
|
|
||
|
Yes, and this comes from old OOP concepts of message passing to objects. These methods are a neat way to be able to add functionality to your
|
||
|
types you define in Go. If you are like me coming from OOP languages, methods are a very helpful construct.
|
||
|
|
||
|
So we've got these people right? And people in this universe are very simple. All they have to identify them is their name, their astrological sign
|
||
|
and their disposition.
|
||
|
|
||
|
```go
|
||
|
type Person struct { name, sign, disposition String }
|
||
|
```
|
||
|
|
||
|
Now a person's name may not change, not their sign, but what about their disposition? I'd say my average day is an emotional rollercoaster! Only kidding,
|
||
|
but we need a way to operate on the person's disposition, let's say based on weather.
|
||
|
|
||
|
```go
|
||
|
// Actually a pointer receiver
|
||
|
func (p *Person) AlterDisposition (weather string) {
|
||
|
switch weather {
|
||
|
case 'rainy':
|
||
|
p.disposition = "sad"
|
||
|
case 'sunny':
|
||
|
p.disposition = "happy"
|
||
|
default:
|
||
|
p.disposition = "meh"
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
That's a funky syntax for this
|
||
|
|
||
|
|
||
|
|