title: How do I check for specific types of error among those returned by ioutil.ReadFile?
body: <p>When I use <code>ioutil</code> to read a file, it may return an error. But if I want to filter some error code, what should I do?  </p>
  
  <pre><code>res, err := ioutil.ReadFile("xxx")
  if err != nil {
      fmt.Println(err.Error())  
  }
  ...
  </code></pre>
  
  <p>In the code snippet above, when a file has no permission, <code>fmt.Println(err.Error())</code> will print <code>"open xxxxx: permission denied</code>.
  If I want to capture this kind error, how can I know that file read failed because permission was denied?</p>
  
  <p>Should I search <code>err.Error()</code> for the string <code>permission denied</code> - this looks ungraceful. Is there any better way?  </p>
  
  <p>Thanks in advance.  </p>
  
  <p><strong>Update</strong>  </p>
  
  <p>After trying @Intermernet solution, I found that it will not hit <code>case os.ErrPermission</code>, it will hit <code>default</code> and print <code>"open xxx: Permission denied"</code>.</p>
  
  <p>@Aedolon solution is ok, <code>os.IsPermission(err)</code> can tell that a file has failed with permission deny.  </p>