title: How to set ulimit -n from a golang program?
body: <p>My purspose was to set ulimit -n from within a golang program so that I do not have to set it globally but restrict it within the program.</p>
  <p>Found systemcalls setrlimit and get rlimit for the same. (<a href="http://linux.die.net/man/2/setrlimit" rel="nofollow noreferrer">http://linux.die.net/man/2/setrlimit</a>)</p>
  <p>But when I tried a sample program for the same I was getting an error saying invalid argument while setting the value.</p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      &quot;fmt&quot;
      &quot;syscall&quot;
  )
  
  func main() {
      var rLimit syscall.Rlimit
  
      err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &amp;rLimit)
  
      if err != nil {
          fmt.Println(&quot;Error Getting Rlimit &quot;, err)
      }
      fmt.Println(rLimit)
  
      rLimit.Max = 999999
      rLimit.Cur = 999999
  
      err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &amp;rLimit)
      if err != nil {
          fmt.Println(&quot;Error Setting Rlimit &quot;, err)
      }
  
      err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &amp;rLimit)
      if err != nil {
          fmt.Println(&quot;Error Getting Rlimit &quot;, err)
      }
      fmt.Println(&quot;Rlimit Final&quot;, rLimit)
  }
  
  </code></pre>
  <p>The output obtained was:</p>
  <pre><code>george@george-Not-Specified ~/work/odesk/progium/trial $ ./getRlimit 
  {4294963002032703 0}
  Error Setting Rlimit  invalid argument
  Rlimit Final {4294963002032703 999999}
  george@george-Not-Specified ~/work/odesk/progium/trial $ sudo ./getRlimit 
  [sudo] password for george: 
  {4294963002032703 0}
  Error Setting Rlimit  invalid argument 
  Rlimit Final {4294963002032703 999999}
  george@george-Not-Specified ~/work/odesk/progium/trial $ uname -a
  Linux george-Not-Specified 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:32:08 UTC 2012 i686 i686 i686 GNU/Linux
  george@george-Not-Specified ~/work/odesk/progium/trial $ 
  </code></pre>
  <p>So I was able to get the rlimit
  Setting the limit failed and returned an error.
  Even though it failed the MAX value got changed when I took the value again but CUR value remains the same.
  Can this error be due to some issue with my kernel or is it a bad program? Where can I find more information and how to deal with an issue like this?</p>
  <p><strong>Update:</strong></p>
  <p>Works after the fix has been made.</p>
  <pre><code>george@george-Not-Specified ~/work/odesk/progium/trial $ go build getRlimit.go 
  george@george-Not-Specified ~/work/odesk/progium/trial $ ./getRlimit 
  {1024 4096}
  Error Setting Rlimit  operation not permitted
  Rlimit Final {1024 4096}
  george@george-Not-Specified ~/work/odesk/progium/trial $ sudo ./getRlimit                                                                                               
  [sudo] password for george: 
  {1024 4096}
  Rlimit Final {999999 999999}
  george@george-Not-Specified ~/work/odesk/progium/trial $ uname -a
  Linux george-Not-Specified 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:32:08 UTC 2012    i686 i686 i686 GNU/Linux
  george@george-Not-Specified ~/work/odesk/progium/trial $ go version
  go version devel +7c42cfa28e24 Tue Jul 30 14:22:14 2013 +1000 linux/386
  </code></pre>