SlideShare uma empresa Scribd logo
1 de 17
Baixar para ler offline
Go: Beyond the Basics
Joey Gibson
Senior Architect
Lancope, Inc.
@joeygibson
Wednesday, July 31, 13
Agenda
• $GOPATH
• Project Organization
• Packages
• Structs
• Methods
• Interfaces
• Regular Expressions
• Functions are First-Class Citizens
• Goroutines and Channels
Wednesday, July 31, 13
$GOPATH
• A series of directories where Go looks for
sources and libraries
• export GOPATH=~/Projects/gobtb:~/Projects/
gotest
• Must contain a src/ directory, with package
directories under that
• Will contain pkg/ directory after doing an install
• go  env -- shows all variables
Wednesday, July 31, 13
Bash
export GOROOT=/Users/<yourname>/Projects/go
export GOPATH=/opt/go-3rdparty:/Users/
<yourname>/Projects/gotest
export PATH=$GOROOT/bin:$PATH:${GOPATH//://
bin:}/bin
Wednesday, July 31, 13
Project Organization
• Project directory should have a src/ subdirectory
• Files in subdirectories of src/ that are in a package (!main)
install to pkg/$GOOS_$GOARCH
• (e.g. src/foo for a “foo” package)
• go  install  foo will install foo.a in pkg/$GOOS_
$GOARCH
• Files in subdirectories of src/ that are in package main,
install to the bin/ directory (e.g. src/bar for a bar command)
• go  install  bar will install bar executable in bin/
Wednesday, July 31, 13
Packages
• Should live in src/foo, src/bar, etc.
• Everything in the directory will get built into a single “thing” (i.e.
foo.a, bar[.exe])
• Can reference each other; don’t use relative references (e.g.
import  “../bar”)
• Can have subpackages
• package quux -- in src/foo/quux/*.go
• imported as “foo/quux”
• referenced as quux.Q()
• Don’t forget aliases -- import xxx foo/quux
Wednesday, July 31, 13
Structs
• Similar to C structs; data only
• Can have methods associated with them
• Can be created on stack or heap; compiler
decides which based on context
• Can be nested
• ... also anonymously...
Wednesday, July 31, 13
Methods
• Look like functions, but have an extra type
clause that specifies which “thing” the
method is for
• The type clause can take a thing, or a
pointer to a thing
• If specified as *thing, no need to provide
address; Go will handle it
Wednesday, July 31, 13
Interfaces
• Specify required methods a type must provide
• Any named type can implement methods to implement
an interface
• No need to declare that a thing implements an interface
• A thing can implement multiple interfaces
• Like duck typing in Ruby, Python, etc., but...
• A compile-time error will occur if trying to pass a thing
that doesn’t implement a required interface
• Convention is to name with -er suffix: Printer, Looper,
Planner, etc.
Wednesday, July 31, 13
Interfaces:
Semi-Generic
• Declaring that a function takes interface{}
will allow the function to accept anything as
a paramerter
• But that means anything at all
Wednesday, July 31, 13
Regular Expressions
• Full support for Perl regex
• Provides compilation, matching, finding,
splitting, and replacing
• Go’s backquotes allow for things like
`Hwllo` without doubling backslashes
Wednesday, July 31, 13
First-class functions
• Can be
• assigned to variables
• passed as parameters to other functions
• stored in maps, arrays, etc.
• created/executed anonymously
Wednesday, July 31, 13
goroutines
• goroutines execute concurrently, on one or more
threads according to availability
• Very lightweight; start with a 4k stack
• Stack grows and shrinks as necessary
• Run in same address space as calling process...
• Are not parallel by default!
• To parallelize: set GOMAXPROCS > 1
Wednesday, July 31, 13
goroutine rule
• “Do not communicate by sharing memory;
instead, share memory by communicating.”
Wednesday, July 31, 13
Channels
• Datatype providing communication and
synchronization between goroutines
• Channels have a type; only one type of thing can be
pushed over a channel
• Created using make()
• Can be buffered or not (defaults to unbuffered)
• Can be declared read-only or write-only...
• Unbuffered means sends and receives block until both
sides are ready
Wednesday, July 31, 13
Channels
• The <-­‐ operator pushes to, or pulls from, a
channel
• The range operator reads from a channel,
until it’s closed
• Close with close()
Wednesday, July 31, 13
Resources
• Testify: https://github.com/stretchr/testify
• Goclipse: https://code.google.com/p/
goclipse/
Wednesday, July 31, 13

Mais conteúdo relacionado

Semelhante a Go: Beyond the Basics

Top 20 Drupal Mistakes newbies make
Top 20 Drupal Mistakes newbies makeTop 20 Drupal Mistakes newbies make
Top 20 Drupal Mistakes newbies makeIztok Smolic
 
Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...
Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...
Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...Puppet
 
Balisage - EXPath Packaging
Balisage - EXPath PackagingBalisage - EXPath Packaging
Balisage - EXPath PackagingFlorent Georges
 
Clojure, Web and Luminus
Clojure, Web and LuminusClojure, Web and Luminus
Clojure, Web and LuminusEdward Tsech
 
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...David Beazley (Dabeaz LLC)
 
Responsive Design and jQuery Mobile
Responsive Design and jQuery MobileResponsive Design and jQuery Mobile
Responsive Design and jQuery MobileTroy Miles
 
Lessons from 4 years of driver develoment
Lessons from 4 years of driver develomentLessons from 4 years of driver develoment
Lessons from 4 years of driver develomentchristkv
 
Getting Started with Go
Getting Started with GoGetting Started with Go
Getting Started with GoSteven Francia
 
Wonders of Golang
Wonders of GolangWonders of Golang
Wonders of GolangKartik Sura
 
Better Python Coding with Prefect Blocks
Better Python Coding with Prefect BlocksBetter Python Coding with Prefect Blocks
Better Python Coding with Prefect BlocksJeff Hale
 
Front-end development automation with Grunt
Front-end development automation with GruntFront-end development automation with Grunt
Front-end development automation with Gruntbenko
 
Cook Up a Runtime with The New OSGi Resolver - Neil Bartlett
Cook Up a Runtime with The New OSGi Resolver - Neil BartlettCook Up a Runtime with The New OSGi Resolver - Neil Bartlett
Cook Up a Runtime with The New OSGi Resolver - Neil Bartlettmfrancis
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboyKenneth Geisshirt
 
Ontology Access Kit_ Workshop Intro Slides.pptx
Ontology Access Kit_ Workshop Intro Slides.pptxOntology Access Kit_ Workshop Intro Slides.pptx
Ontology Access Kit_ Workshop Intro Slides.pptxChris Mungall
 
Yoshiwara-AMATYC2018
Yoshiwara-AMATYC2018Yoshiwara-AMATYC2018
Yoshiwara-AMATYC2018byoshiwara
 

Semelhante a Go: Beyond the Basics (20)

Top 20 Drupal Mistakes newbies make
Top 20 Drupal Mistakes newbies makeTop 20 Drupal Mistakes newbies make
Top 20 Drupal Mistakes newbies make
 
Python
PythonPython
Python
 
Python
PythonPython
Python
 
Python
PythonPython
Python
 
Python_book.pdf
Python_book.pdfPython_book.pdf
Python_book.pdf
 
Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...
Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...
Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...
 
Balisage - EXPath Packaging
Balisage - EXPath PackagingBalisage - EXPath Packaging
Balisage - EXPath Packaging
 
Clojure, Web and Luminus
Clojure, Web and LuminusClojure, Web and Luminus
Clojure, Web and Luminus
 
Ready to go
Ready to goReady to go
Ready to go
 
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
 
Responsive Design and jQuery Mobile
Responsive Design and jQuery MobileResponsive Design and jQuery Mobile
Responsive Design and jQuery Mobile
 
Lessons from 4 years of driver develoment
Lessons from 4 years of driver develomentLessons from 4 years of driver develoment
Lessons from 4 years of driver develoment
 
Getting Started with Go
Getting Started with GoGetting Started with Go
Getting Started with Go
 
Wonders of Golang
Wonders of GolangWonders of Golang
Wonders of Golang
 
Better Python Coding with Prefect Blocks
Better Python Coding with Prefect BlocksBetter Python Coding with Prefect Blocks
Better Python Coding with Prefect Blocks
 
Front-end development automation with Grunt
Front-end development automation with GruntFront-end development automation with Grunt
Front-end development automation with Grunt
 
Cook Up a Runtime with The New OSGi Resolver - Neil Bartlett
Cook Up a Runtime with The New OSGi Resolver - Neil BartlettCook Up a Runtime with The New OSGi Resolver - Neil Bartlett
Cook Up a Runtime with The New OSGi Resolver - Neil Bartlett
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
Ontology Access Kit_ Workshop Intro Slides.pptx
Ontology Access Kit_ Workshop Intro Slides.pptxOntology Access Kit_ Workshop Intro Slides.pptx
Ontology Access Kit_ Workshop Intro Slides.pptx
 
Yoshiwara-AMATYC2018
Yoshiwara-AMATYC2018Yoshiwara-AMATYC2018
Yoshiwara-AMATYC2018
 

Último

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 

Último (20)

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 

Go: Beyond the Basics

  • 1. Go: Beyond the Basics Joey Gibson Senior Architect Lancope, Inc. @joeygibson Wednesday, July 31, 13
  • 2. Agenda • $GOPATH • Project Organization • Packages • Structs • Methods • Interfaces • Regular Expressions • Functions are First-Class Citizens • Goroutines and Channels Wednesday, July 31, 13
  • 3. $GOPATH • A series of directories where Go looks for sources and libraries • export GOPATH=~/Projects/gobtb:~/Projects/ gotest • Must contain a src/ directory, with package directories under that • Will contain pkg/ directory after doing an install • go  env -- shows all variables Wednesday, July 31, 13
  • 5. Project Organization • Project directory should have a src/ subdirectory • Files in subdirectories of src/ that are in a package (!main) install to pkg/$GOOS_$GOARCH • (e.g. src/foo for a “foo” package) • go  install  foo will install foo.a in pkg/$GOOS_ $GOARCH • Files in subdirectories of src/ that are in package main, install to the bin/ directory (e.g. src/bar for a bar command) • go  install  bar will install bar executable in bin/ Wednesday, July 31, 13
  • 6. Packages • Should live in src/foo, src/bar, etc. • Everything in the directory will get built into a single “thing” (i.e. foo.a, bar[.exe]) • Can reference each other; don’t use relative references (e.g. import  “../bar”) • Can have subpackages • package quux -- in src/foo/quux/*.go • imported as “foo/quux” • referenced as quux.Q() • Don’t forget aliases -- import xxx foo/quux Wednesday, July 31, 13
  • 7. Structs • Similar to C structs; data only • Can have methods associated with them • Can be created on stack or heap; compiler decides which based on context • Can be nested • ... also anonymously... Wednesday, July 31, 13
  • 8. Methods • Look like functions, but have an extra type clause that specifies which “thing” the method is for • The type clause can take a thing, or a pointer to a thing • If specified as *thing, no need to provide address; Go will handle it Wednesday, July 31, 13
  • 9. Interfaces • Specify required methods a type must provide • Any named type can implement methods to implement an interface • No need to declare that a thing implements an interface • A thing can implement multiple interfaces • Like duck typing in Ruby, Python, etc., but... • A compile-time error will occur if trying to pass a thing that doesn’t implement a required interface • Convention is to name with -er suffix: Printer, Looper, Planner, etc. Wednesday, July 31, 13
  • 10. Interfaces: Semi-Generic • Declaring that a function takes interface{} will allow the function to accept anything as a paramerter • But that means anything at all Wednesday, July 31, 13
  • 11. Regular Expressions • Full support for Perl regex • Provides compilation, matching, finding, splitting, and replacing • Go’s backquotes allow for things like `Hwllo` without doubling backslashes Wednesday, July 31, 13
  • 12. First-class functions • Can be • assigned to variables • passed as parameters to other functions • stored in maps, arrays, etc. • created/executed anonymously Wednesday, July 31, 13
  • 13. goroutines • goroutines execute concurrently, on one or more threads according to availability • Very lightweight; start with a 4k stack • Stack grows and shrinks as necessary • Run in same address space as calling process... • Are not parallel by default! • To parallelize: set GOMAXPROCS > 1 Wednesday, July 31, 13
  • 14. goroutine rule • “Do not communicate by sharing memory; instead, share memory by communicating.” Wednesday, July 31, 13
  • 15. Channels • Datatype providing communication and synchronization between goroutines • Channels have a type; only one type of thing can be pushed over a channel • Created using make() • Can be buffered or not (defaults to unbuffered) • Can be declared read-only or write-only... • Unbuffered means sends and receives block until both sides are ready Wednesday, July 31, 13
  • 16. Channels • The <-­‐ operator pushes to, or pulls from, a channel • The range operator reads from a channel, until it’s closed • Close with close() Wednesday, July 31, 13
  • 17. Resources • Testify: https://github.com/stretchr/testify • Goclipse: https://code.google.com/p/ goclipse/ Wednesday, July 31, 13