SlideShare a Scribd company logo
•  Introduce	
  yourself	
  
	
  
1	
  
•  Going	
  to	
  tell	
  a	
  quick	
  story.	
  
2	
  
There	
  are	
  anima;ons	
  of	
  Nathan	
  and	
  I	
  coming	
  in	
  and	
  out	
  during	
  the	
  story.	
  
	
  
•  Nathan	
  is	
  a	
  friend	
  of	
  mine	
  who	
  contributes	
  to	
  the	
  fsno;fy	
  package,	
  he	
  has	
  done	
  
some	
  great	
  things	
  for	
  the	
  community.	
  
	
  
3	
  
•  Nathan	
  was	
  online	
  in	
  Slack	
  asking	
  ques;ons	
  about	
  error	
  handling.	
  
•  So,	
  I	
  did	
  what	
  any	
  friend	
  would	
  do,	
  I	
  jumped	
  into	
  the	
  conversa;on.	
  
•  AFer	
  talking	
  for	
  a	
  bit	
  he	
  showed	
  me	
  some	
  code	
  similar	
  to	
  this.	
  
•  When	
  I	
  saw	
  this	
  it	
  just	
  didn’t	
  feel	
  right.	
  
•  This	
  seemed	
  way	
  to	
  complicated	
  but	
  I	
  couldn’t	
  put	
  my	
  finger	
  on	
  it.	
  
4	
  
•  I	
  realized	
  aFer	
  months	
  of	
  coding	
  in	
  Go	
  I	
  had	
  no	
  idea	
  how	
  errors	
  worked.	
  
•  I	
  saw	
  errors	
  in	
  Go	
  as	
  string	
  messages,	
  there	
  had	
  to	
  be	
  more.	
  
•  Testament	
  to	
  Go	
  that	
  you	
  don’t	
  need	
  to	
  know	
  all	
  the	
  details.	
  
•  Bad	
  experience	
  with	
  custom	
  error	
  types	
  in	
  C++	
  and	
  C#	
  
•  Custom	
  error	
  types	
  were	
  not	
  prevalent	
  but	
  they	
  did	
  exist	
  in	
  the	
  standard	
  library.	
  
•  To	
  be	
  effec;ve,	
  let’s	
  learn	
  how	
  errors	
  really	
  work	
  in	
  Go.	
  
**	
  Next	
  Slide	
  –	
  Think	
  About	
  This	
  **	
  
	
  
5	
  
•  This	
  is	
  what	
  I	
  want	
  you	
  to	
  take	
  away	
  from	
  this	
  talk.	
  
6	
  
•  Share	
  two	
  thoughts.	
  
•  This	
  is	
  the	
  basis	
  for	
  how	
  I	
  look	
  at	
  error	
  handling.	
  
	
  
	
  
7	
  
How	
  we	
  decide	
  to	
  create	
  our	
  error	
  values	
  should	
  be	
  based	
  on	
  this	
  idea.	
  
	
  
8	
  
Ask	
  yourself	
  these	
  ques;ons.	
  
	
  
**	
  Next	
  –	
  Working	
  With	
  Error	
  Values	
  **	
  
	
  
9	
  
10	
  
•  Helps	
  to	
  clarify	
  how	
  to	
  think	
  about	
  errors	
  in	
  Go.	
  
•  Errors	
  are	
  values,	
  values	
  that	
  need	
  to	
  be	
  handled	
  as	
  errors	
  happen.	
  
•  Not	
  as	
  an	
  aFer	
  thought	
  or	
  something	
  to	
  be	
  handled	
  later.	
  
11	
  
•  A	
  common	
  pa]ern	
  in	
  Go.	
  
•  The	
  error	
  value	
  returned	
  is	
  checked	
  against	
  the	
  value	
  of	
  nil.	
  
•  If	
  the	
  error	
  value	
  is	
  not	
  nil,	
  then	
  an	
  error	
  occurred	
  and	
  all	
  other	
  values	
  are	
  ignored.	
  
•  The	
  error	
  is	
  handled	
  immediately	
  and	
  decisions	
  are	
  made	
  as	
  to	
  how	
  to	
  proceed.	
  
**	
  Next	
  Slide	
  –	
  Standard	
  Library	
  Support	
  **	
  
12	
  
13	
  
•  The	
  error	
  interface	
  as	
  declared	
  by	
  the	
  standard	
  library.	
  
•  Contains	
  a	
  single	
  method	
  called	
  Error	
  that	
  returns	
  a	
  string.	
  
•  The	
  interface	
  is	
  built	
  into	
  the	
  language.	
  
•  Though	
  it	
  is	
  unexported	
  by	
  its	
  name,	
  we	
  s;ll	
  have	
  access	
  to	
  it.	
  
14	
  
•  Default	
  implementa;on	
  of	
  the	
  error	
  interface.	
  
•  errorString	
  is	
  declared	
  in	
  the	
  errors	
  package.	
  
•  Unexported	
  struct	
  with	
  a	
  single	
  unexported	
  field.	
  
•  The	
  implementa;on	
  uses	
  a	
  pointer	
  receiver	
  and	
  returns	
  the	
  value	
  of	
  s.	
  
•  Most	
  used	
  concrete	
  type	
  in	
  the	
  standard	
  library	
  for	
  error	
  values.	
  
•  The	
  nature	
  of	
  interfaces	
  make	
  this	
  struct	
  very	
  effec;ve	
  in	
  handling	
  errors.	
  
15	
  
•  Use	
  New	
  func	
  to	
  create	
  error	
  values	
  using	
  the	
  struct	
  as	
  the	
  concrete	
  type.	
  
•  Takes	
  a	
  string	
  and	
  returns	
  a	
  pointer	
  of	
  type	
  errorString.	
  
•  The	
  return	
  type	
  for	
  the	
  New	
  func;on	
  is	
  an	
  interface	
  value	
  of	
  type	
  error.	
  
•  We	
  return	
  a	
  pointer	
  of	
  type	
  errorString.	
  
•  The	
  caller	
  receives	
  is	
  an	
  interface	
  value	
  of	
  type	
  error.	
  
•  What	
  does	
  the	
  value	
  that	
  the	
  caller	
  receives	
  actually	
  looks	
  like?	
  
16	
  
•  Interface	
  value	
  is	
  always	
  a	
  two	
  machine	
  word	
  value.	
  
•  It	
  stores	
  a	
  concrete	
  type	
  value	
  that	
  implements	
  the	
  interface.	
  
•  When	
  the	
  New	
  func;on	
  is	
  an	
  error	
  interface	
  value	
  is	
  created	
  on	
  the	
  return	
  and	
  the	
  
errorString	
  pointer	
  is	
  stored	
  within	
  it.	
  
•  The	
  language	
  support	
  abstracts	
  this	
  implementa;on	
  away	
  and	
  makes	
  working	
  with	
  
these	
  values	
  intui;ve.	
  
17	
  
•  Just	
  as	
  a	
  note.	
  
•  This	
  is	
  the	
  value	
  that	
  is	
  actually	
  returned	
  for	
  nil.	
  
•  An	
  interface	
  value	
  is	
  always	
  returned.	
  
•  We	
  have	
  like	
  types	
  present	
  on	
  both	
  sides	
  of	
  the	
  compare	
  statement.	
  
18	
  
•  Second	
  way	
  to	
  create	
  an	
  error	
  interface	
  value	
  based	
  on	
  the	
  errorString	
  struct.	
  
•  Use	
  when	
  you	
  need	
  a	
  forma]ed	
  error,	
  usually	
  with	
  local	
  variables.	
  
•  No;ce	
  the	
  use	
  of	
  the	
  New	
  func;on	
  from	
  the	
  errors	
  package.	
  
**	
  Next	
  –	
  Use	
  In	
  Standard	
  Library	
  **	
  
19	
  
20	
  
•  Let’s	
  learn	
  a	
  technique	
  used	
  in	
  the	
  standard	
  library	
  to	
  help	
  us	
  iden;ty	
  specific	
  
errors.	
  
•  The	
  error	
  interface	
  variables	
  above	
  are	
  declared	
  in	
  the	
  bufio	
  package.	
  
•  They	
  provide	
  support	
  for	
  iden;fying	
  specific	
  errors.	
  
•  This	
  works	
  when	
  the	
  error	
  messages	
  are	
  sta;c.	
  
21	
  
•  How	
  to	
  use	
  the	
  error	
  interface	
  variables	
  to	
  iden;fy	
  which	
  error	
  was	
  returned	
  by	
  
the	
  Peek	
  func;on.	
  
•  The	
  Peek	
  func;on	
  can	
  return	
  either	
  the	
  ErrNega;veCount	
  or	
  ErrBufferFull	
  error	
  
variable.	
  
•  We	
  use	
  the	
  same	
  variables	
  to	
  iden;fy	
  the	
  specific	
  error	
  returned.	
  
•  Make	
  a	
  more	
  informed	
  error	
  handling	
  decision.	
  
22	
  
•  How	
  to	
  use	
  the	
  error	
  interface	
  variables	
  to	
  iden;fy	
  which	
  error	
  was	
  returned	
  by	
  
the	
  Peek	
  func;on.	
  
•  The	
  Peek	
  func;on	
  can	
  return	
  either	
  the	
  ErrNega;veCount	
  or	
  ErrBufferFull	
  error	
  
variable.	
  
•  We	
  use	
  the	
  same	
  variables	
  to	
  iden;fy	
  the	
  specific	
  error	
  returned.	
  
•  Make	
  a	
  more	
  informed	
  error	
  handling	
  decision.	
  
23	
  
•  Other	
  packages	
  like	
  the	
  io	
  package	
  use	
  the	
  same	
  technique	
  
•  I	
  am	
  sure	
  you	
  have	
  compared	
  the	
  returned	
  error	
  interface	
  value	
  to	
  EOF.	
  
24	
  
•  Here	
  is	
  the	
  implementa;on	
  of	
  the	
  ReadAtLeast	
  func;on	
  from	
  the	
  io	
  package.	
  
•  We	
  see	
  how	
  these	
  variables	
  are	
  being	
  returned	
  
•  We	
  see	
  how	
  the	
  EOF	
  variable	
  is	
  used	
  internally	
  to	
  check	
  for	
  that	
  error.	
  
•  When	
  you	
  need	
  to	
  make	
  a	
  decision	
  about	
  a	
  specific	
  error,	
  check	
  if	
  an	
  error	
  
interface	
  variable	
  exists	
  to	
  help	
  you.	
  
	
  
**	
  Next	
  –	
  Custom	
  Error	
  Types	
  **	
  
25	
  
•  How	
  to	
  use	
  the	
  error	
  interface	
  variables	
  to	
  iden;fy	
  which	
  error	
  was	
  returned	
  by	
  
the	
  Peek	
  func;on.	
  
•  The	
  Peek	
  func;on	
  can	
  return	
  either	
  the	
  ErrNega;veCount	
  or	
  ErrBufferFull	
  error	
  
variable.	
  
•  We	
  use	
  the	
  same	
  variables	
  to	
  iden;fy	
  the	
  specific	
  error	
  returned.	
  
•  Make	
  a	
  more	
  informed	
  error	
  handling	
  decision.	
  
27	
  
•  This	
  is	
  a	
  custom	
  error	
  type	
  implemented	
  within	
  the	
  net	
  package.	
  
28	
  
•  This	
  is	
  a	
  custom	
  error	
  type	
  implemented	
  within	
  the	
  net	
  package.	
  
•  The	
  name	
  of	
  the	
  custom	
  error	
  type	
  follows	
  the	
  Go	
  naming	
  conven;on	
  for	
  custom	
  
error	
  types.	
  
•  The	
  custom	
  error	
  type	
  should	
  provide	
  context	
  associated	
  with	
  the	
  error	
  to	
  help	
  
the	
  caller	
  make	
  a	
  more	
  informed	
  decision.	
  
•  The	
  first	
  three	
  fields	
  provide	
  context	
  about	
  the	
  network	
  opera;on	
  being	
  
performed	
  when	
  an	
  error	
  occurs.	
  
•  The	
  fourth	
  field	
  contains	
  the	
  actual	
  error	
  that	
  occurred.	
  
•  The	
  custom	
  error	
  type	
  exists	
  to	
  provide	
  the	
  context	
  associated	
  with	
  the	
  error.	
  
29	
  
•  This	
  is	
  the	
  implementa;on	
  of	
  the	
  error	
  interface	
  for	
  OpError.	
  
•  The	
  context	
  associated	
  with	
  the	
  error	
  is	
  used	
  to	
  produce	
  a	
  more	
  detailed	
  error	
  
message.	
  
•  The	
  context	
  enhances	
  the	
  details	
  and	
  produces	
  an	
  error	
  message	
  with	
  more	
  
meaning.	
  
30	
  
•  This	
  is	
  a	
  custom	
  error	
  type	
  implemented	
  in	
  the	
  json	
  package.	
  
•  This	
  custom	
  error	
  type	
  is	
  used	
  to	
  report	
  errors	
  that	
  occur	
  when	
  a	
  value	
  can’t	
  be	
  
decoded	
  into	
  a	
  specific	
  Go	
  type.	
  
•  In	
  this	
  case,	
  the	
  type	
  itself	
  provides	
  the	
  context	
  and	
  maintains	
  state	
  associated	
  
with	
  the	
  error.	
  
31	
  
•  The	
  implementa;on	
  of	
  the	
  error	
  interface	
  takes	
  the	
  state	
  associated	
  with	
  the	
  
error	
  and	
  produces	
  a	
  detailed	
  and	
  context	
  rich	
  message.	
  
32	
  
•  This	
  custom	
  error	
  type	
  is	
  used	
  to	
  report	
  when	
  there	
  are	
  invalid	
  arguments	
  passed	
  
into	
  an	
  unmarshal	
  call.	
  
•  In	
  this	
  case,	
  only	
  the	
  type	
  informa;on	
  associated	
  with	
  the	
  bad	
  argument	
  is	
  
required	
  to	
  be	
  stored.	
  
33	
  
•  Here	
  we	
  see	
  the	
  implementa;on	
  of	
  the	
  error	
  interface	
  for	
  this	
  custom	
  error	
  type.	
  
•  Again	
  the	
  state	
  associated	
  with	
  the	
  error	
  is	
  used	
  to	
  produce	
  an	
  error	
  message	
  with	
  
more	
  meaning	
  and	
  context.	
  
34	
  
In	
  these	
  cases	
  
•  A	
  custom	
  error	
  type	
  provided	
  context	
  beyond	
  what	
  the	
  errors	
  package	
  
could	
  provide.	
  
•  The	
  context	
  helped	
  the	
  caller	
  make	
  a	
  more	
  informed	
  decision	
  about	
  the	
  
error	
  
	
  
**	
  Next	
  –	
  Concrete	
  Type	
  Iden;fica;on	
  **	
  
35	
  
36	
  
•  This	
  method	
  is	
  called	
  by	
  the	
  exported	
  Unmarshal	
  func;on.	
  
•  Method	
  has	
  the	
  poten;al	
  to	
  return	
  error	
  values	
  of	
  different	
  concrete	
  types.	
  
•  Pointers	
  of	
  type	
  UnmarshalTypeError,	
  InvalidUnmarshalError	
  or	
  errorString.	
  
•  Each	
  error	
  contains	
  a	
  different	
  context	
  that	
  is	
  important	
  to	
  know.	
  
•  We	
  want	
  to	
  make	
  an	
  informed	
  decision	
  on	
  how	
  to	
  handle	
  the	
  specific	
  error.	
  
37	
  
•  Iden;fy	
  the	
  concrete	
  type	
  is	
  for	
  the	
  stored	
  value	
  inside	
  the	
  error	
  interface	
  value.	
  
•  You	
  can	
  do	
  this,	
  but	
  there	
  is	
  a	
  be]er	
  way.	
  
38	
  
•  The	
  switch	
  statement	
  supports	
  this	
  special	
  syntax.	
  
•  An	
  interface	
  type	
  conversion	
  using	
  the	
  keyword	
  type.	
  
•  Declare	
  case	
  statements	
  based	
  on	
  the	
  different	
  concrete	
  types	
  we	
  want	
  to	
  check	
  
for.	
  
39	
  
40	
  
41	
  
42	
  
•  Introduce	
  yourself	
  
•  I	
  realized	
  aFer	
  months	
  of	
  coding	
  in	
  Go	
  I	
  had	
  no	
  idea	
  how	
  errors	
  worked.	
  
•  I	
  saw	
  errors	
  in	
  Go	
  as	
  string	
  messages,	
  there	
  had	
  to	
  be	
  more.	
  
•  Testament	
  to	
  Go	
  that	
  you	
  don’t	
  need	
  to	
  know	
  all	
  the	
  details.	
  
•  Bad	
  experience	
  with	
  custom	
  error	
  types	
  in	
  C++	
  and	
  C#	
  
•  Custom	
  error	
  types	
  were	
  not	
  prevalent	
  but	
  they	
  did	
  exist	
  in	
  the	
  standard	
  library.	
  
•  To	
  be	
  effec;ve,	
  let’s	
  learn	
  how	
  errors	
  really	
  work	
  in	
  Go.	
  
43	
  

More Related Content

What's hot

Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
Joxean Koret
 
Ch4 Expressions
Ch4 ExpressionsCh4 Expressions
Ch4 Expressions
SzeChingChen
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
Hossam Ghareeb
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
SzeChingChen
 
Introduction to Scala Implicits, Pimp my library and Typeclasses
Introduction to Scala Implicits, Pimp my library and TypeclassesIntroduction to Scala Implicits, Pimp my library and Typeclasses
Introduction to Scala Implicits, Pimp my library and Typeclasses
Jordi Pradel
 
Ch10 Program Organization
Ch10 Program OrganizationCh10 Program Organization
Ch10 Program Organization
SzeChingChen
 
Half-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code RecoveryHalf-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code Recovery
Joxean Koret
 
Packer Genetics: The selfish code
Packer Genetics: The selfish codePacker Genetics: The selfish code
Packer Genetics: The selfish code
jduart
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
Cihad Horuzoğlu
 
Erlang, LFE, Joxa and Elixir: Established and Emerging Languages in the Erlan...
Erlang, LFE, Joxa and Elixir: Established and Emerging Languages in the Erlan...Erlang, LFE, Joxa and Elixir: Established and Emerging Languages in the Erlan...
Erlang, LFE, Joxa and Elixir: Established and Emerging Languages in the Erlan...
Brian Troutwine
 
ShaREing Is Caring
ShaREing Is CaringShaREing Is Caring
ShaREing Is Caring
sporst
 
(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercises(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercises
Nico Ludwig
 

What's hot (12)

Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
Pigaios: A Tool for Diffing Source Codes against Binaries (Hacktivity 2018)
 
Ch4 Expressions
Ch4 ExpressionsCh4 Expressions
Ch4 Expressions
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Introduction to Scala Implicits, Pimp my library and Typeclasses
Introduction to Scala Implicits, Pimp my library and TypeclassesIntroduction to Scala Implicits, Pimp my library and Typeclasses
Introduction to Scala Implicits, Pimp my library and Typeclasses
 
Ch10 Program Organization
Ch10 Program OrganizationCh10 Program Organization
Ch10 Program Organization
 
Half-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code RecoveryHalf-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code Recovery
 
Packer Genetics: The selfish code
Packer Genetics: The selfish codePacker Genetics: The selfish code
Packer Genetics: The selfish code
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
Erlang, LFE, Joxa and Elixir: Established and Emerging Languages in the Erlan...
Erlang, LFE, Joxa and Elixir: Established and Emerging Languages in the Erlan...Erlang, LFE, Joxa and Elixir: Established and Emerging Languages in the Erlan...
Erlang, LFE, Joxa and Elixir: Established and Emerging Languages in the Erlan...
 
ShaREing Is Caring
ShaREing Is CaringShaREing Is Caring
ShaREing Is Caring
 
(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercises(4) cpp automatic arrays_pointers_c-strings_exercises
(4) cpp automatic arrays_pointers_c-strings_exercises
 

Similar to Gotham go2014

CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
Michael Heron
 
12.6-12.9.pptx
12.6-12.9.pptx12.6-12.9.pptx
12.6-12.9.pptx
WinterSnow16
 
Python-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptxPython-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptx
muzammildev46gmailco
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
ssusere336f4
 
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGEINTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
RathnaM16
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
MaaReddySanjiv
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
ssusere336f4
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
VAIBHAVKADAGANCHI
 
Module-1.pptx
Module-1.pptxModule-1.pptx
Module-1.pptx
Manohar Nelli
 
Unit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and FlowchartUnit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and Flowchart
Bom Khati
 
System software module 1 presentation file
System software module 1 presentation fileSystem software module 1 presentation file
System software module 1 presentation file
jithujithin657
 
DAA Unit 1.pdf
DAA Unit 1.pdfDAA Unit 1.pdf
DAA Unit 1.pdf
Nirmalavenkatachalam
 
General Talk on Pointers
General Talk on PointersGeneral Talk on Pointers
General Talk on Pointers
emartinez.romero
 
Error handling and debugging in vb
Error handling and debugging in vbError handling and debugging in vb
Error handling and debugging in vb
Salim M
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
Michael Heron
 
Cis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programming
Hamad Odhabi
 
Building reusable components with generics and protocols
Building reusable components with generics and protocolsBuilding reusable components with generics and protocols
Building reusable components with generics and protocols
Donny Wals
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
Mukund Trivedi
 
Exception handling
Exception handlingException handling
Exception handling
Minal Maniar
 
Cis 1403 lab5_loops
Cis 1403 lab5_loopsCis 1403 lab5_loops
Cis 1403 lab5_loops
Hamad Odhabi
 

Similar to Gotham go2014 (20)

CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
 
12.6-12.9.pptx
12.6-12.9.pptx12.6-12.9.pptx
12.6-12.9.pptx
 
Python-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptxPython-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptx
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
 
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGEINTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Module-1.pptx
Module-1.pptxModule-1.pptx
Module-1.pptx
 
Unit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and FlowchartUnit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and Flowchart
 
System software module 1 presentation file
System software module 1 presentation fileSystem software module 1 presentation file
System software module 1 presentation file
 
DAA Unit 1.pdf
DAA Unit 1.pdfDAA Unit 1.pdf
DAA Unit 1.pdf
 
General Talk on Pointers
General Talk on PointersGeneral Talk on Pointers
General Talk on Pointers
 
Error handling and debugging in vb
Error handling and debugging in vbError handling and debugging in vb
Error handling and debugging in vb
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
 
Cis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programming
 
Building reusable components with generics and protocols
Building reusable components with generics and protocolsBuilding reusable components with generics and protocols
Building reusable components with generics and protocols
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
Exception handling
Exception handlingException handling
Exception handling
 
Cis 1403 lab5_loops
Cis 1403 lab5_loopsCis 1403 lab5_loops
Cis 1403 lab5_loops
 

Recently uploaded

Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
maazsz111
 

Recently uploaded (20)

Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
 

Gotham go2014

  • 2. •  Going  to  tell  a  quick  story.   2  
  • 3. There  are  anima;ons  of  Nathan  and  I  coming  in  and  out  during  the  story.     •  Nathan  is  a  friend  of  mine  who  contributes  to  the  fsno;fy  package,  he  has  done   some  great  things  for  the  community.     3  
  • 4. •  Nathan  was  online  in  Slack  asking  ques;ons  about  error  handling.   •  So,  I  did  what  any  friend  would  do,  I  jumped  into  the  conversa;on.   •  AFer  talking  for  a  bit  he  showed  me  some  code  similar  to  this.   •  When  I  saw  this  it  just  didn’t  feel  right.   •  This  seemed  way  to  complicated  but  I  couldn’t  put  my  finger  on  it.   4  
  • 5. •  I  realized  aFer  months  of  coding  in  Go  I  had  no  idea  how  errors  worked.   •  I  saw  errors  in  Go  as  string  messages,  there  had  to  be  more.   •  Testament  to  Go  that  you  don’t  need  to  know  all  the  details.   •  Bad  experience  with  custom  error  types  in  C++  and  C#   •  Custom  error  types  were  not  prevalent  but  they  did  exist  in  the  standard  library.   •  To  be  effec;ve,  let’s  learn  how  errors  really  work  in  Go.   **  Next  Slide  –  Think  About  This  **     5  
  • 6. •  This  is  what  I  want  you  to  take  away  from  this  talk.   6  
  • 7. •  Share  two  thoughts.   •  This  is  the  basis  for  how  I  look  at  error  handling.       7  
  • 8. How  we  decide  to  create  our  error  values  should  be  based  on  this  idea.     8  
  • 9. Ask  yourself  these  ques;ons.     **  Next  –  Working  With  Error  Values  **     9  
  • 10. 10  
  • 11. •  Helps  to  clarify  how  to  think  about  errors  in  Go.   •  Errors  are  values,  values  that  need  to  be  handled  as  errors  happen.   •  Not  as  an  aFer  thought  or  something  to  be  handled  later.   11  
  • 12. •  A  common  pa]ern  in  Go.   •  The  error  value  returned  is  checked  against  the  value  of  nil.   •  If  the  error  value  is  not  nil,  then  an  error  occurred  and  all  other  values  are  ignored.   •  The  error  is  handled  immediately  and  decisions  are  made  as  to  how  to  proceed.   **  Next  Slide  –  Standard  Library  Support  **   12  
  • 13. 13  
  • 14. •  The  error  interface  as  declared  by  the  standard  library.   •  Contains  a  single  method  called  Error  that  returns  a  string.   •  The  interface  is  built  into  the  language.   •  Though  it  is  unexported  by  its  name,  we  s;ll  have  access  to  it.   14  
  • 15. •  Default  implementa;on  of  the  error  interface.   •  errorString  is  declared  in  the  errors  package.   •  Unexported  struct  with  a  single  unexported  field.   •  The  implementa;on  uses  a  pointer  receiver  and  returns  the  value  of  s.   •  Most  used  concrete  type  in  the  standard  library  for  error  values.   •  The  nature  of  interfaces  make  this  struct  very  effec;ve  in  handling  errors.   15  
  • 16. •  Use  New  func  to  create  error  values  using  the  struct  as  the  concrete  type.   •  Takes  a  string  and  returns  a  pointer  of  type  errorString.   •  The  return  type  for  the  New  func;on  is  an  interface  value  of  type  error.   •  We  return  a  pointer  of  type  errorString.   •  The  caller  receives  is  an  interface  value  of  type  error.   •  What  does  the  value  that  the  caller  receives  actually  looks  like?   16  
  • 17. •  Interface  value  is  always  a  two  machine  word  value.   •  It  stores  a  concrete  type  value  that  implements  the  interface.   •  When  the  New  func;on  is  an  error  interface  value  is  created  on  the  return  and  the   errorString  pointer  is  stored  within  it.   •  The  language  support  abstracts  this  implementa;on  away  and  makes  working  with   these  values  intui;ve.   17  
  • 18. •  Just  as  a  note.   •  This  is  the  value  that  is  actually  returned  for  nil.   •  An  interface  value  is  always  returned.   •  We  have  like  types  present  on  both  sides  of  the  compare  statement.   18  
  • 19. •  Second  way  to  create  an  error  interface  value  based  on  the  errorString  struct.   •  Use  when  you  need  a  forma]ed  error,  usually  with  local  variables.   •  No;ce  the  use  of  the  New  func;on  from  the  errors  package.   **  Next  –  Use  In  Standard  Library  **   19  
  • 20. 20  
  • 21. •  Let’s  learn  a  technique  used  in  the  standard  library  to  help  us  iden;ty  specific   errors.   •  The  error  interface  variables  above  are  declared  in  the  bufio  package.   •  They  provide  support  for  iden;fying  specific  errors.   •  This  works  when  the  error  messages  are  sta;c.   21  
  • 22. •  How  to  use  the  error  interface  variables  to  iden;fy  which  error  was  returned  by   the  Peek  func;on.   •  The  Peek  func;on  can  return  either  the  ErrNega;veCount  or  ErrBufferFull  error   variable.   •  We  use  the  same  variables  to  iden;fy  the  specific  error  returned.   •  Make  a  more  informed  error  handling  decision.   22  
  • 23. •  How  to  use  the  error  interface  variables  to  iden;fy  which  error  was  returned  by   the  Peek  func;on.   •  The  Peek  func;on  can  return  either  the  ErrNega;veCount  or  ErrBufferFull  error   variable.   •  We  use  the  same  variables  to  iden;fy  the  specific  error  returned.   •  Make  a  more  informed  error  handling  decision.   23  
  • 24. •  Other  packages  like  the  io  package  use  the  same  technique   •  I  am  sure  you  have  compared  the  returned  error  interface  value  to  EOF.   24  
  • 25. •  Here  is  the  implementa;on  of  the  ReadAtLeast  func;on  from  the  io  package.   •  We  see  how  these  variables  are  being  returned   •  We  see  how  the  EOF  variable  is  used  internally  to  check  for  that  error.   •  When  you  need  to  make  a  decision  about  a  specific  error,  check  if  an  error   interface  variable  exists  to  help  you.     **  Next  –  Custom  Error  Types  **   25  
  • 26. •  How  to  use  the  error  interface  variables  to  iden;fy  which  error  was  returned  by   the  Peek  func;on.   •  The  Peek  func;on  can  return  either  the  ErrNega;veCount  or  ErrBufferFull  error   variable.   •  We  use  the  same  variables  to  iden;fy  the  specific  error  returned.   •  Make  a  more  informed  error  handling  decision.  
  • 27. 27  
  • 28. •  This  is  a  custom  error  type  implemented  within  the  net  package.   28  
  • 29. •  This  is  a  custom  error  type  implemented  within  the  net  package.   •  The  name  of  the  custom  error  type  follows  the  Go  naming  conven;on  for  custom   error  types.   •  The  custom  error  type  should  provide  context  associated  with  the  error  to  help   the  caller  make  a  more  informed  decision.   •  The  first  three  fields  provide  context  about  the  network  opera;on  being   performed  when  an  error  occurs.   •  The  fourth  field  contains  the  actual  error  that  occurred.   •  The  custom  error  type  exists  to  provide  the  context  associated  with  the  error.   29  
  • 30. •  This  is  the  implementa;on  of  the  error  interface  for  OpError.   •  The  context  associated  with  the  error  is  used  to  produce  a  more  detailed  error   message.   •  The  context  enhances  the  details  and  produces  an  error  message  with  more   meaning.   30  
  • 31. •  This  is  a  custom  error  type  implemented  in  the  json  package.   •  This  custom  error  type  is  used  to  report  errors  that  occur  when  a  value  can’t  be   decoded  into  a  specific  Go  type.   •  In  this  case,  the  type  itself  provides  the  context  and  maintains  state  associated   with  the  error.   31  
  • 32. •  The  implementa;on  of  the  error  interface  takes  the  state  associated  with  the   error  and  produces  a  detailed  and  context  rich  message.   32  
  • 33. •  This  custom  error  type  is  used  to  report  when  there  are  invalid  arguments  passed   into  an  unmarshal  call.   •  In  this  case,  only  the  type  informa;on  associated  with  the  bad  argument  is   required  to  be  stored.   33  
  • 34. •  Here  we  see  the  implementa;on  of  the  error  interface  for  this  custom  error  type.   •  Again  the  state  associated  with  the  error  is  used  to  produce  an  error  message  with   more  meaning  and  context.   34  
  • 35. In  these  cases   •  A  custom  error  type  provided  context  beyond  what  the  errors  package   could  provide.   •  The  context  helped  the  caller  make  a  more  informed  decision  about  the   error     **  Next  –  Concrete  Type  Iden;fica;on  **   35  
  • 36. 36  
  • 37. •  This  method  is  called  by  the  exported  Unmarshal  func;on.   •  Method  has  the  poten;al  to  return  error  values  of  different  concrete  types.   •  Pointers  of  type  UnmarshalTypeError,  InvalidUnmarshalError  or  errorString.   •  Each  error  contains  a  different  context  that  is  important  to  know.   •  We  want  to  make  an  informed  decision  on  how  to  handle  the  specific  error.   37  
  • 38. •  Iden;fy  the  concrete  type  is  for  the  stored  value  inside  the  error  interface  value.   •  You  can  do  this,  but  there  is  a  be]er  way.   38  
  • 39. •  The  switch  statement  supports  this  special  syntax.   •  An  interface  type  conversion  using  the  keyword  type.   •  Declare  case  statements  based  on  the  different  concrete  types  we  want  to  check   for.   39  
  • 40. 40  
  • 41. 41  
  • 42. 42  
  • 43. •  Introduce  yourself   •  I  realized  aFer  months  of  coding  in  Go  I  had  no  idea  how  errors  worked.   •  I  saw  errors  in  Go  as  string  messages,  there  had  to  be  more.   •  Testament  to  Go  that  you  don’t  need  to  know  all  the  details.   •  Bad  experience  with  custom  error  types  in  C++  and  C#   •  Custom  error  types  were  not  prevalent  but  they  did  exist  in  the  standard  library.   •  To  be  effec;ve,  let’s  learn  how  errors  really  work  in  Go.   43  

Editor's Notes

  1. Introduce yourself
  2. Going to tell a quick story.
  3. There are animations of Nathan and I coming in and out during the story. Nathan is a friend of mine who contributes to the fsnotify package, he has done some great things for the community.
  4. Nathan was online in Slack asking questions about error handling. So, I did what any friend would do, I jumped into the conversation. After talking for a bit he showed me some code similar to this. When I saw this it just didn’t feel right. This seemed way to complicated but I couldn’t put my finger on it.
  5. I realized after months of coding in Go I had no idea how errors worked. I saw errors in Go as string messages, there had to be more. Testament to Go that you don’t need to know all the details. Bad experience with custom error types in C++ and C# Custom error types were not prevalent but they did exist in the standard library. To be effective, let’s learn how errors really work in Go. ** Next Slide – Think About This **
  6. This is what I want you to take away from this talk.
  7. Share two thoughts. This is the basis for how I look at error handling.
  8. How we decide to create our error values should be based on this idea.
  9. Ask yourself these questions. ** Next – Working With Error Values **
  10. Helps to clarify how to think about errors in Go. Errors are values, values that need to be handled as errors happen. Not as an after thought or something to be handled later.
  11. A common pattern in Go. The error value returned is checked against the value of nil. If the error value is not nil, then an error occurred and all other values are ignored. The error is handled immediately and decisions are made as to how to proceed. ** Next Slide – Standard Library Support **
  12. The error interface as declared by the standard library. Contains a single method called Error that returns a string. The interface is built into the language. Though it is unexported by its name, we still have access to it.
  13. Default implementation of the error interface. errorString is declared in the errors package. Unexported struct with a single unexported field. The implementation uses a pointer receiver and returns the value of s. Most used concrete type in the standard library for error values. The nature of interfaces make this struct very effective in handling errors.
  14. Use New func to create error values using the struct as the concrete type. Takes a string and returns a pointer of type errorString. The return type for the New function is an interface value of type error. We return a pointer of type errorString. The caller receives is an interface value of type error. What does the value that the caller receives actually looks like?
  15. Interface value is always a two machine word value. It stores a concrete type value that implements the interface. When the New function is an error interface value is created on the return and the errorString pointer is stored within it. The language support abstracts this implementation away and makes working with these values intuitive.
  16. Just as a note. This is the value that is actually returned for nil. An interface value is always returned. We have like types present on both sides of the compare statement.
  17. Second way to create an error interface value based on the errorString struct. Use when you need a formatted error, usually with local variables. Notice the use of the New function from the errors package. ** Next – Use In Standard Library **
  18. Let’s learn a technique used in the standard library to help us identity specific errors. The error interface variables above are declared in the bufio package. They provide support for identifying specific errors. This works when the error messages are static.
  19. How to use the error interface variables to identify which error was returned by the Peek function. The Peek function can return either the ErrNegativeCount or ErrBufferFull error variable. We use the same variables to identify the specific error returned. Make a more informed error handling decision.
  20. How to use the error interface variables to identify which error was returned by the Peek function. The Peek function can return either the ErrNegativeCount or ErrBufferFull error variable. We use the same variables to identify the specific error returned. Make a more informed error handling decision.
  21. Other packages like the io package use the same technique I am sure you have compared the returned error interface value to EOF.
  22. Here is the implementation of the ReadAtLeast function from the io package. We see how these variables are being returned We see how the EOF variable is used internally to check for that error. When you need to make a decision about a specific error, check if an error interface variable exists to help you. ** Next – Custom Error Types **
  23. How to use the error interface variables to identify which error was returned by the Peek function. The Peek function can return either the ErrNegativeCount or ErrBufferFull error variable. We use the same variables to identify the specific error returned. Make a more informed error handling decision.
  24. This is a custom error type implemented within the net package.
  25. This is a custom error type implemented within the net package. The name of the custom error type follows the Go naming convention for custom error types. The custom error type should provide context associated with the error to help the caller make a more informed decision. The first three fields provide context about the network operation being performed when an error occurs. The fourth field contains the actual error that occurred. The custom error type exists to provide the context associated with the error.
  26. This is the implementation of the error interface for OpError. The context associated with the error is used to produce a more detailed error message. The context enhances the details and produces an error message with more meaning.
  27. This is a custom error type implemented in the json package. This custom error type is used to report errors that occur when a value can’t be decoded into a specific Go type. In this case, the type itself provides the context and maintains state associated with the error.
  28. The implementation of the error interface takes the state associated with the error and produces a detailed and context rich message.
  29. This custom error type is used to report when there are invalid arguments passed into an unmarshal call. In this case, only the type information associated with the bad argument is required to be stored.
  30. Here we see the implementation of the error interface for this custom error type. Again the state associated with the error is used to produce an error message with more meaning and context.
  31. In these cases A custom error type provided context beyond what the errors package could provide. The context helped the caller make a more informed decision about the error ** Next – Concrete Type Identification **
  32. This method is called by the exported Unmarshal function. Method has the potential to return error values of different concrete types. Pointers of type UnmarshalTypeError, InvalidUnmarshalError or errorString. Each error contains a different context that is important to know. We want to make an informed decision on how to handle the specific error.
  33. Identify the concrete type is for the stored value inside the error interface value. You can do this, but there is a better way.
  34. The switch statement supports this special syntax. An interface type conversion using the keyword type. Declare case statements based on the different concrete types we want to check for.
  35. Introduce yourself I realized after months of coding in Go I had no idea how errors worked. I saw errors in Go as string messages, there had to be more. Testament to Go that you don’t need to know all the details. Bad experience with custom error types in C++ and C# Custom error types were not prevalent but they did exist in the standard library. To be effective, let’s learn how errors really work in Go.