SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
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

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Dernier

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
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
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
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
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
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
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
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
 
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
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Dernier (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
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!
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
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
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
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
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
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
 
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
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

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