SlideShare a Scribd company logo
1 of 43
Download to read offline
•  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
 
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 languageHossam Ghareeb
 
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 TypeclassesJordi Pradel
 
Ch10 Program Organization
Ch10 Program OrganizationCh10 Program Organization
Ch10 Program OrganizationSzeChingChen
 
Half-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code RecoveryHalf-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code RecoveryJoxean Koret
 
Packer Genetics: The selfish code
Packer Genetics: The selfish codePacker Genetics: The selfish code
Packer Genetics: The selfish codejduart
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming LanguageCihad 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 Caringsporst
 
(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_exercisesNico 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

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.pptxmuzammildev46gmailco
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxssusere336f4
 
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 LANGUAGERathnaM16
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxssusere336f4
 
Unit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and FlowchartUnit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and FlowchartBom Khati
 
System software module 1 presentation file
System software module 1 presentation fileSystem software module 1 presentation file
System software module 1 presentation filejithujithin657
 
Error handling and debugging in vb
Error handling and debugging in vbError handling and debugging in vb
Error handling and debugging in vbSalim M
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method OverloadingMichael 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 programmingHamad 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 protocolsDonny Wals
 
Exception handling
Exception handlingException handling
Exception handlingMinal Maniar
 
Cis 1403 lab5_loops
Cis 1403 lab5_loopsCis 1403 lab5_loops
Cis 1403 lab5_loopsHamad 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
 
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
 
Class_X_PYTHON_J.pdf
Class_X_PYTHON_J.pdfClass_X_PYTHON_J.pdf
Class_X_PYTHON_J.pdf
 

Recently uploaded

Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentMahmoud Rabie
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
WomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneWomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneUiPathCommunity
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 

Recently uploaded (20)

Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career Development
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
WomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneWomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyone
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
How Tech Giants Cut Corners to Harvest Data for A.I.
How Tech Giants Cut Corners to Harvest Data for A.I.How Tech Giants Cut Corners to Harvest Data for A.I.
How Tech Giants Cut Corners to Harvest Data for A.I.
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 

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.