SlideShare a Scribd company logo
Go for Object 

Oriented Programmers
!
or
!
Object Oriented Programming 

Without Objects
• Author of Hugo, Cobra,
Viper & More
• Chief Developer
Advocate for MongoDB
• Gopher
@spf13
— txxxxd
“Most of the appeal for me is not
the features that Go has, but
rather the features that have
been intentionally left out.”
— Rob Pike
“Why would you have a
language that is not
theoretically exciting?
Because it’s very useful.”
“Objects” 

in Go
Does Go have Objects?
• Go lacks Classes
• Go lacks “Objects”
What is an
Object?
– Steve Francia
“An object is an
abstract data type that
has state (data) and
behavior (code).”
type Rect struct {
width int
height int
}
Type Declaration (Struct)
func (r *Rect) Area() int {
return r.width * r.height
}
Declaring a Method
func main() {
r := Rect{width: 10, height: 5}
fmt.Println("area: ", r.Area())
}
In Action
type Rects []Rect
Type Declaration (Slice)
func (rs Rects) Area() int {
var a int
for _, r := range rs {
a += r.Area()
}
return a
}
Declaring a Method
func main() {
r := Rect{width: 10, height: 5}
x := Rect{width: 7, height:10}
rs := Rects{r, x}
fmt.Println("r's area: ", r.Area())
fmt.Println("x's area: ", x.Area())
fmt.Println("total area: ", rs.Area())
}
In Action
http://play.golang.org/p/G1OWXPGvc3
type Foo func() int
Type Declaration (Func)
func (f Foo) Add(x int) int {
return f() + x
}
Declaring a Method
func main() {
var x Foo
!
x = func() int { return 1 }
!
fmt.Println(x())
fmt.Println(x.Add(3))
}
In Action
http://play.golang.org/p/YGrdCG3SlI
Go Has
“Objects”
“Object Oriented”
Go
– Wikipedia
A language is usually considered object-based
if it includes the basic capabilities for an
object: identity, properties, and attributes.


A language is considered object-oriented if it
is object-based and also has the capability of
polymorphism and inheritance.
Go is Object Based.
!
Is it OO?
Inheritance
• Provides reuse of objects
• Classes are created in hierarchies
• Inheritance lets the structure and
methods in one class pass down the
hierarchy
Go’s approach
• Go explicitly avoided inheritance
• Go strictly follows the composition over
inheritance principle
• Composition through embedded types
Composition
• Provides reuse of Objects
• One object is declared by including other
objects
• Composition lets the structure and methods
in one class be pulled into another
– Steve Francia
Inheritance passes “knowledge” down
!
Composition pulls “knowledge” up
type Person struct {
Name string
Address
}
!
!
type Address struct {
Number string
Street string
City string
State string
Zip string
}
Embedding Types
Inner Type
func (a *Address) String() string {
return a.Number+" "+a.Street+"n"+

a.City+", "+a.State+" "+a.Zip+"n"
}
Declaring a Method
func main() {
p := Person{
Name: "Steve",
Address: Address{
Number: "13",
Street: "Main",
City: "Gotham",
State: "NY",
Zip: "01313",
},
}
}
Declare using Composite Literal
func main() {
p := Person{
Name: "Steve",
Address: Address{
Number: "13",
Street: "Main",
City: "Gotham",
State: "NY",
Zip: "01313",
},
}
fmt.Println(p.String())
In Action
http://play.golang.org/p/9beVY9jNlW
Promotion
• Promotion looks to see if a single inner type can
satisify the request and “promotes” it
• Embedded fields & methods are “promoted”
• Promotion only occurs during usage, not declaration
• Promoted methods are considered for interface
adherance
func (a *Address) String() string {
return a.Number+" "+a.Street+"n"+

a.City+", "+a.State+" "+a.Zip+"n"
}
!
func (p *Person) String() string {
return p.Name + "n" + p.Address.String()
}
Not Overloading
func main() {
p := Person{
Name: "Steve",
Address: Address{
Number: "13",
Street: "Main",
City: "Gotham",
State: "NY",
Zip: "01313",
},
}
!
fmt.Println(p.String())
fmt.Println(p.Address.String())
}
Both Methods Available
http://play.golang.org/p/Aui0nGa5Xi
func isValidAddress(a *Address) bool {
return a.Street != ""
}
!
func main() {
p := Person{ Name: "Steve", Address: Address{ Number: "13", Street:
"Main", City: "Gotham", State: "NY", Zip: "01313"}}
!
fmt.Println(isValidAddress(p)) 

// cannot use p (type Person) as type Address 

// in argument to isValidAddress
fmt.Println(isValidAddress(p.Address))
}
Types Remain Distinct
http://play.golang.org/p/KYjXZxNBcQ
Promotion is
NOT Subtyping
Polymorphism
• “The provision of a single interface to
entities of different types”
• Typically implmented via Generics,
Overloading and/or Subtyping
Go’s approach
• Go explicitly avoided subtyping &
overloading
• Go does not provide Generics (yet)
• Go’s interface provide polymorphic
capabilities
Interfaces
• A list of required methods
• Structural vs Nominal typing
• ‘‘If something can do this, then it can be
used here”
• Convention is call it a Something-er
type Shaper interface {
Area() int
}
Interface Declaration
func Describe(s Shaper) {
fmt.Println("Area is:", s.Area())
}
Using Interface as Param Type
func main() {
r := &Rect{width: 10, height: 5}
x := &Rect{width: 7, height: 10}
rs := &Rects{r, x}
Describe(r)
Describe(x)
Describe(rs)
}
In Action
http://play.golang.org/p/WL77LihUwi
–James Gosling (creator of Java)
“If you could do Java over again, what would you
change?” “I’d leave out classes,” he replied. After the
laughter died down, he explained that the real
problem wasn’t classes per se, but rather
implementation inheritance (the extends
relationship). Interface inheritance (the implements
relationship) is preferable. You should avoid
implementation inheritance whenever possible.
Go Interfaces are
based on
implementation,
not declaration
The Power of
Interfaces
type Reader interface {
Read(p []byte) (n int, err error)
}
io.Reader
io.Reader
• Interface
• Read reads up to len(p) bytes into p
• Returns the # of bytes read & any error
• Does not dictate how Read() is implemented
• Used by os.File, bytes.Buffer, net.Conn,
http.Request.Body, loads more
type Writer interface {
Write(p []byte) (n int, err error)
}
io.Writer
io.Writer
• Interface
• Write writes up to len(p) bytes into p
• Returns the # of bytes written & any error
• Does not dictate how Write() is implemented
• Used by os.File, bytes.Buffer, net.Conn,
http.Response.Body, loads more
func MarshalGzippedJSON(r io.Reader, 

v interface{}) error {
raw, err := gzip.NewReader(r)
if err != nil {
return err
}
return json.NewDecoder(raw).Decode(&v)
}
io.Reader in Action
f, err := os.Open("myfile.json.gz")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
m = make(map[string]interface{})
MarshalGzippedJSON(f, &m)
Reading a json.gz file
Practical Interoperability
• Gzip.NewReader(io.Reader)
• Works on files, http requests, byte buffers,
network connections, …anything you create
• Nothing special needed in gzip to be able to
do this… Simply call Read(n) and leave the
abstracting to the implementor
func main() {
resp, err := http.Get("...")
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
out, err := os.Create("filename.ext")
if err != nil {
log.Fatalln(err)
}
defer out.Close()
io.Copy(out, resp.Body) // out io.Writer, resp.Body io.Reader
}
Pipe http response to file
Go
— Steve Jobs
Simple can be harder than
complex: You have to work hard
to get your thinking clean to
make it simple. But it's worth it
in the end because once you get
there, you can move mountains.
Go is simple,
pratical &
wonderful
Go build
something
great
Thank You

More Related Content

What's hot (20)

Introduction to python history and platforms
Introduction to python history and platforms
Kirti Verma
 
Mother board (Part's of Motherboard)
Mother board (Part's of Motherboard)
Coder Tech
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratch
linuxlab_conf
 
Python functions
Python functions
Prof. Dr. K. Adisesha
 
NI221 - Fundamentals of Computer
NI221 - Fundamentals of Computer
paulcaspe
 
Embedded_Linux_Booting
Embedded_Linux_Booting
Rashila Rr
 
Types of Computer Memories
Types of Computer Memories
Bilal Ahmad
 
Introduction to yocto
Introduction to yocto
Alex Gonzalez
 
Yocto Project introduction
Yocto Project introduction
Yi-Hsiu Hsu
 
CPU
CPU
Ray Mkindo
 
Embedded Linux on ARM
Embedded Linux on ARM
Emertxe Information Technologies Pvt Ltd
 
U-Boot Porting on New Hardware
U-Boot Porting on New Hardware
RuggedBoardGroup
 
Kernel Module Programming
Kernel Module Programming
Saurabh Bangad
 
Bios
Bios
Vishal Gohel
 
Introduction to char device driver
Introduction to char device driver
Vandana Salve
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)
Macpaul Lin
 
Bios y UEFI
Bios y UEFI
Carlos Torres
 
BKK16-201 Play Ready OPTEE Integration with Secure Video Path lhg-1
BKK16-201 Play Ready OPTEE Integration with Secure Video Path lhg-1
Linaro
 
Top 5 Frameworks In Python | Django, Web2Py, Flask, Bottle, CherryPy | Edureka
Top 5 Frameworks In Python | Django, Web2Py, Flask, Bottle, CherryPy | Edureka
Edureka!
 
Linux directory structure by jitu mistry
Linux directory structure by jitu mistry
JITU MISTRY
 
Introduction to python history and platforms
Introduction to python history and platforms
Kirti Verma
 
Mother board (Part's of Motherboard)
Mother board (Part's of Motherboard)
Coder Tech
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratch
linuxlab_conf
 
NI221 - Fundamentals of Computer
NI221 - Fundamentals of Computer
paulcaspe
 
Embedded_Linux_Booting
Embedded_Linux_Booting
Rashila Rr
 
Types of Computer Memories
Types of Computer Memories
Bilal Ahmad
 
Introduction to yocto
Introduction to yocto
Alex Gonzalez
 
Yocto Project introduction
Yocto Project introduction
Yi-Hsiu Hsu
 
U-Boot Porting on New Hardware
U-Boot Porting on New Hardware
RuggedBoardGroup
 
Kernel Module Programming
Kernel Module Programming
Saurabh Bangad
 
Introduction to char device driver
Introduction to char device driver
Vandana Salve
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)
Macpaul Lin
 
BKK16-201 Play Ready OPTEE Integration with Secure Video Path lhg-1
BKK16-201 Play Ready OPTEE Integration with Secure Video Path lhg-1
Linaro
 
Top 5 Frameworks In Python | Django, Web2Py, Flask, Bottle, CherryPy | Edureka
Top 5 Frameworks In Python | Django, Web2Py, Flask, Bottle, CherryPy | Edureka
Edureka!
 
Linux directory structure by jitu mistry
Linux directory structure by jitu mistry
JITU MISTRY
 

Viewers also liked (20)

Painless Data Storage with MongoDB & Go
Painless Data Storage with MongoDB & Go
Steven Francia
 
7 Common Mistakes in Go (2015)
7 Common Mistakes in Go (2015)
Steven Francia
 
The Future of the Operating System - Keynote LinuxCon 2015
The Future of the Operating System - Keynote LinuxCon 2015
Steven Francia
 
Getting Started with Go
Getting Started with Go
Steven Francia
 
7 Common mistakes in Go and when to avoid them
7 Common mistakes in Go and when to avoid them
Steven Francia
 
What every successful open source project needs
What every successful open source project needs
Steven Francia
 
Building Awesome CLI apps in Go
Building Awesome CLI apps in Go
Steven Francia
 
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Steven Francia
 
Big data for the rest of us
Big data for the rest of us
Steven Francia
 
MongoDB, Hadoop and humongous data - MongoSV 2012
MongoDB, Hadoop and humongous data - MongoSV 2012
Steven Francia
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB Tutorial
Steven Francia
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
A Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to Go
Matt Stine
 
Object-oriented programming
Object-oriented programming
Neelesh Shukla
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Alena Holligan
 
Mongo db - How we use Go and MongoDB by Sam Helman
Mongo db - How we use Go and MongoDB by Sam Helman
Hakka Labs
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
ppd1961
 
Object Oriented Concept
Object Oriented Concept
smj
 
Need of object oriented programming
Need of object oriented programming
Amar Jukuntla
 
Architecting for the Cloud using NetflixOSS - Codemash Workshop
Architecting for the Cloud using NetflixOSS - Codemash Workshop
Sudhir Tonse
 
Painless Data Storage with MongoDB & Go
Painless Data Storage with MongoDB & Go
Steven Francia
 
7 Common Mistakes in Go (2015)
7 Common Mistakes in Go (2015)
Steven Francia
 
The Future of the Operating System - Keynote LinuxCon 2015
The Future of the Operating System - Keynote LinuxCon 2015
Steven Francia
 
Getting Started with Go
Getting Started with Go
Steven Francia
 
7 Common mistakes in Go and when to avoid them
7 Common mistakes in Go and when to avoid them
Steven Francia
 
What every successful open source project needs
What every successful open source project needs
Steven Francia
 
Building Awesome CLI apps in Go
Building Awesome CLI apps in Go
Steven Francia
 
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Steven Francia
 
Big data for the rest of us
Big data for the rest of us
Steven Francia
 
MongoDB, Hadoop and humongous data - MongoSV 2012
MongoDB, Hadoop and humongous data - MongoSV 2012
Steven Francia
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB Tutorial
Steven Francia
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
A Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to Go
Matt Stine
 
Object-oriented programming
Object-oriented programming
Neelesh Shukla
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Alena Holligan
 
Mongo db - How we use Go and MongoDB by Sam Helman
Mongo db - How we use Go and MongoDB by Sam Helman
Hakka Labs
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
ppd1961
 
Object Oriented Concept
Object Oriented Concept
smj
 
Need of object oriented programming
Need of object oriented programming
Amar Jukuntla
 
Architecting for the Cloud using NetflixOSS - Codemash Workshop
Architecting for the Cloud using NetflixOSS - Codemash Workshop
Sudhir Tonse
 
Ad

Similar to Go for Object Oriented Programmers or Object Oriented Programming without Objects (20)

Introduction to Go
Introduction to Go
Lorenzo Aiello
 
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
Eugene Yokota
 
Json the-x-in-ajax1588
Json the-x-in-ajax1588
Ramamohan Chokkam
 
Go Workshop Day 1
Go Workshop Day 1
Hiroaki Kamei
 
C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607
Kevin Hazzard
 
Tuga IT 2017 - What's new in C# 7
Tuga IT 2017 - What's new in C# 7
Paulo Morgado
 
Kotlin Austin Droids April 14 2016
Kotlin Austin Droids April 14 2016
DesertJames
 
Javascript
Javascript
Sunil Thakur
 
Denis Lebedev, Swift
Denis Lebedev, Swift
Yandex
 
json.ppt download for free for college project
json.ppt download for free for college project
AmitSharma397241
 
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
Joshua Lawrence
 
Scala the language matters
Scala the language matters
Xiaojun REN
 
Go react codelab
Go react codelab
Alberto Jesús Gutiérrez Juanes
 
Dynamic Python
Dynamic Python
Chui-Wen Chiu
 
Json at work overview and ecosystem-v2.0
Json at work overview and ecosystem-v2.0
Boulder Java User's Group
 
型ヒントについて考えよう!
型ヒントについて考えよう!
Yusuke Miyazaki
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
 
The Kotlin Programming Language
The Kotlin Programming Language
intelliyole
 
devLink - What's New in C# 4?
devLink - What's New in C# 4?
Kevin Pilch
 
Golang slidesaudrey
Golang slidesaudrey
Audrey Lim
 
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
Eugene Yokota
 
C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607
Kevin Hazzard
 
Tuga IT 2017 - What's new in C# 7
Tuga IT 2017 - What's new in C# 7
Paulo Morgado
 
Kotlin Austin Droids April 14 2016
Kotlin Austin Droids April 14 2016
DesertJames
 
Denis Lebedev, Swift
Denis Lebedev, Swift
Yandex
 
json.ppt download for free for college project
json.ppt download for free for college project
AmitSharma397241
 
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
Joshua Lawrence
 
Scala the language matters
Scala the language matters
Xiaojun REN
 
型ヒントについて考えよう!
型ヒントについて考えよう!
Yusuke Miyazaki
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
 
The Kotlin Programming Language
The Kotlin Programming Language
intelliyole
 
devLink - What's New in C# 4?
devLink - What's New in C# 4?
Kevin Pilch
 
Golang slidesaudrey
Golang slidesaudrey
Audrey Lim
 
Ad

More from Steven Francia (19)

State of the Gopher Nation - Golang - August 2017
State of the Gopher Nation - Golang - August 2017
Steven Francia
 
Modern Database Systems (for Genealogy)
Modern Database Systems (for Genealogy)
Steven Francia
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
Steven Francia
 
Future of data
Future of data
Steven Francia
 
Replication, Durability, and Disaster Recovery
Replication, Durability, and Disaster Recovery
Steven Francia
 
Multi Data Center Strategies
Multi Data Center Strategies
Steven Francia
 
NoSQL databases and managing big data
NoSQL databases and managing big data
Steven Francia
 
MongoDB, Hadoop and Humongous Data
MongoDB, Hadoop and Humongous Data
Steven Francia
 
MongoDB and hadoop
MongoDB and hadoop
Steven Francia
 
MongoDB for Genealogy
MongoDB for Genealogy
Steven Francia
 
Hybrid MongoDB and RDBMS Applications
Hybrid MongoDB and RDBMS Applications
Steven Francia
 
Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011
Steven Francia
 
MongoDB, E-commerce and Transactions
MongoDB, E-commerce and Transactions
Steven Francia
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
Steven Francia
 
MongoDB and PHP ZendCon 2011
MongoDB and PHP ZendCon 2011
Steven Francia
 
MongoDB
MongoDB
Steven Francia
 
Blending MongoDB and RDBMS for ecommerce
Blending MongoDB and RDBMS for ecommerce
Steven Francia
 
Augmenting RDBMS with MongoDB for ecommerce
Augmenting RDBMS with MongoDB for ecommerce
Steven Francia
 
MongoDB and Ecommerce : A perfect combination
MongoDB and Ecommerce : A perfect combination
Steven Francia
 
State of the Gopher Nation - Golang - August 2017
State of the Gopher Nation - Golang - August 2017
Steven Francia
 
Modern Database Systems (for Genealogy)
Modern Database Systems (for Genealogy)
Steven Francia
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
Steven Francia
 
Replication, Durability, and Disaster Recovery
Replication, Durability, and Disaster Recovery
Steven Francia
 
Multi Data Center Strategies
Multi Data Center Strategies
Steven Francia
 
NoSQL databases and managing big data
NoSQL databases and managing big data
Steven Francia
 
MongoDB, Hadoop and Humongous Data
MongoDB, Hadoop and Humongous Data
Steven Francia
 
Hybrid MongoDB and RDBMS Applications
Hybrid MongoDB and RDBMS Applications
Steven Francia
 
Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011
Steven Francia
 
MongoDB, E-commerce and Transactions
MongoDB, E-commerce and Transactions
Steven Francia
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
Steven Francia
 
MongoDB and PHP ZendCon 2011
MongoDB and PHP ZendCon 2011
Steven Francia
 
Blending MongoDB and RDBMS for ecommerce
Blending MongoDB and RDBMS for ecommerce
Steven Francia
 
Augmenting RDBMS with MongoDB for ecommerce
Augmenting RDBMS with MongoDB for ecommerce
Steven Francia
 
MongoDB and Ecommerce : A perfect combination
MongoDB and Ecommerce : A perfect combination
Steven Francia
 

Recently uploaded (20)

FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 

Go for Object Oriented Programmers or Object Oriented Programming without Objects

  • 1. Go for Object 
 Oriented Programmers ! or ! Object Oriented Programming 
 Without Objects
  • 2. • Author of Hugo, Cobra, Viper & More • Chief Developer Advocate for MongoDB • Gopher @spf13
  • 3. — txxxxd “Most of the appeal for me is not the features that Go has, but rather the features that have been intentionally left out.”
  • 4. — Rob Pike “Why would you have a language that is not theoretically exciting? Because it’s very useful.”
  • 6. Does Go have Objects? • Go lacks Classes • Go lacks “Objects”
  • 8. – Steve Francia “An object is an abstract data type that has state (data) and behavior (code).”
  • 9. type Rect struct { width int height int } Type Declaration (Struct)
  • 10. func (r *Rect) Area() int { return r.width * r.height } Declaring a Method
  • 11. func main() { r := Rect{width: 10, height: 5} fmt.Println("area: ", r.Area()) } In Action
  • 12. type Rects []Rect Type Declaration (Slice)
  • 13. func (rs Rects) Area() int { var a int for _, r := range rs { a += r.Area() } return a } Declaring a Method
  • 14. func main() { r := Rect{width: 10, height: 5} x := Rect{width: 7, height:10} rs := Rects{r, x} fmt.Println("r's area: ", r.Area()) fmt.Println("x's area: ", x.Area()) fmt.Println("total area: ", rs.Area()) } In Action http://play.golang.org/p/G1OWXPGvc3
  • 15. type Foo func() int Type Declaration (Func)
  • 16. func (f Foo) Add(x int) int { return f() + x } Declaring a Method
  • 17. func main() { var x Foo ! x = func() int { return 1 } ! fmt.Println(x()) fmt.Println(x.Add(3)) } In Action http://play.golang.org/p/YGrdCG3SlI
  • 20. – Wikipedia A language is usually considered object-based if it includes the basic capabilities for an object: identity, properties, and attributes. 
 A language is considered object-oriented if it is object-based and also has the capability of polymorphism and inheritance.
  • 21. Go is Object Based. ! Is it OO?
  • 22. Inheritance • Provides reuse of objects • Classes are created in hierarchies • Inheritance lets the structure and methods in one class pass down the hierarchy
  • 23. Go’s approach • Go explicitly avoided inheritance • Go strictly follows the composition over inheritance principle • Composition through embedded types
  • 24. Composition • Provides reuse of Objects • One object is declared by including other objects • Composition lets the structure and methods in one class be pulled into another
  • 25. – Steve Francia Inheritance passes “knowledge” down ! Composition pulls “knowledge” up
  • 26. type Person struct { Name string Address } ! ! type Address struct { Number string Street string City string State string Zip string } Embedding Types Inner Type
  • 27. func (a *Address) String() string { return a.Number+" "+a.Street+"n"+
 a.City+", "+a.State+" "+a.Zip+"n" } Declaring a Method
  • 28. func main() { p := Person{ Name: "Steve", Address: Address{ Number: "13", Street: "Main", City: "Gotham", State: "NY", Zip: "01313", }, } } Declare using Composite Literal
  • 29. func main() { p := Person{ Name: "Steve", Address: Address{ Number: "13", Street: "Main", City: "Gotham", State: "NY", Zip: "01313", }, } fmt.Println(p.String()) In Action http://play.golang.org/p/9beVY9jNlW
  • 30. Promotion • Promotion looks to see if a single inner type can satisify the request and “promotes” it • Embedded fields & methods are “promoted” • Promotion only occurs during usage, not declaration • Promoted methods are considered for interface adherance
  • 31. func (a *Address) String() string { return a.Number+" "+a.Street+"n"+
 a.City+", "+a.State+" "+a.Zip+"n" } ! func (p *Person) String() string { return p.Name + "n" + p.Address.String() } Not Overloading
  • 32. func main() { p := Person{ Name: "Steve", Address: Address{ Number: "13", Street: "Main", City: "Gotham", State: "NY", Zip: "01313", }, } ! fmt.Println(p.String()) fmt.Println(p.Address.String()) } Both Methods Available http://play.golang.org/p/Aui0nGa5Xi
  • 33. func isValidAddress(a *Address) bool { return a.Street != "" } ! func main() { p := Person{ Name: "Steve", Address: Address{ Number: "13", Street: "Main", City: "Gotham", State: "NY", Zip: "01313"}} ! fmt.Println(isValidAddress(p)) 
 // cannot use p (type Person) as type Address 
 // in argument to isValidAddress fmt.Println(isValidAddress(p.Address)) } Types Remain Distinct http://play.golang.org/p/KYjXZxNBcQ
  • 35. Polymorphism • “The provision of a single interface to entities of different types” • Typically implmented via Generics, Overloading and/or Subtyping
  • 36. Go’s approach • Go explicitly avoided subtyping & overloading • Go does not provide Generics (yet) • Go’s interface provide polymorphic capabilities
  • 37. Interfaces • A list of required methods • Structural vs Nominal typing • ‘‘If something can do this, then it can be used here” • Convention is call it a Something-er
  • 38. type Shaper interface { Area() int } Interface Declaration
  • 39. func Describe(s Shaper) { fmt.Println("Area is:", s.Area()) } Using Interface as Param Type
  • 40. func main() { r := &Rect{width: 10, height: 5} x := &Rect{width: 7, height: 10} rs := &Rects{r, x} Describe(r) Describe(x) Describe(rs) } In Action http://play.golang.org/p/WL77LihUwi
  • 41. –James Gosling (creator of Java) “If you could do Java over again, what would you change?” “I’d leave out classes,” he replied. After the laughter died down, he explained that the real problem wasn’t classes per se, but rather implementation inheritance (the extends relationship). Interface inheritance (the implements relationship) is preferable. You should avoid implementation inheritance whenever possible.
  • 42. Go Interfaces are based on implementation, not declaration
  • 44. type Reader interface { Read(p []byte) (n int, err error) } io.Reader
  • 45. io.Reader • Interface • Read reads up to len(p) bytes into p • Returns the # of bytes read & any error • Does not dictate how Read() is implemented • Used by os.File, bytes.Buffer, net.Conn, http.Request.Body, loads more
  • 46. type Writer interface { Write(p []byte) (n int, err error) } io.Writer
  • 47. io.Writer • Interface • Write writes up to len(p) bytes into p • Returns the # of bytes written & any error • Does not dictate how Write() is implemented • Used by os.File, bytes.Buffer, net.Conn, http.Response.Body, loads more
  • 48. func MarshalGzippedJSON(r io.Reader, 
 v interface{}) error { raw, err := gzip.NewReader(r) if err != nil { return err } return json.NewDecoder(raw).Decode(&v) } io.Reader in Action
  • 49. f, err := os.Open("myfile.json.gz") if err != nil { log.Fatalln(err) } defer f.Close() m = make(map[string]interface{}) MarshalGzippedJSON(f, &m) Reading a json.gz file
  • 50. Practical Interoperability • Gzip.NewReader(io.Reader) • Works on files, http requests, byte buffers, network connections, …anything you create • Nothing special needed in gzip to be able to do this… Simply call Read(n) and leave the abstracting to the implementor
  • 51. func main() { resp, err := http.Get("...") if err != nil { log.Fatalln(err) } defer resp.Body.Close() out, err := os.Create("filename.ext") if err != nil { log.Fatalln(err) } defer out.Close() io.Copy(out, resp.Body) // out io.Writer, resp.Body io.Reader } Pipe http response to file
  • 52. Go
  • 53. — Steve Jobs Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains.
  • 54. Go is simple, pratical & wonderful