title: runtime: goroutine stack exceeds 1000000000-byte limit, fatal error: stack overflow on printing a nested struct
body: <p>I have a nested struct.</p>
  
  <pre><code>type ConfigOne struct {
      // Daemon section from config file.
      Daemon daemon
  }
  type daemon struct {
      Loglevel int
      Logfile string
  }
  </code></pre>
  
  <p>And I have a <code>String() string</code> method on that type, which I am trying to return the nested struct elements as </p>
  
  <pre><code>func (c ConfigOne)String()  string{
      return fmt.Sprintf("%+v\n", c)
  }
  </code></pre>
  
  <p>When I am trying to print it as </p>
  
  <pre><code>c := &amp;modules.ConfigOne{}
  c.Daemon.Loglevel = 1
  c.Daemon.Logfile = "/tmp/test.log"
  modules.Logger.Infoln(c.String())
  </code></pre>
  
  <p>I am getting the error</p>
  
  <blockquote>
    <p>runtime: goroutine stack exceeds 1000000000-byte limit
    fatal error: stack overflow</p>
    
    <p>runtime stack:
    runtime.throw(0x6ea3b7, 0xe)
    ...</p>
  </blockquote>
  
  <p>After going through the error, I could see repeated lines similar to the below one </p>
  
  <blockquote>
    <p>modules/structs.go:31 +0x77 fp=0xc440100398 sp=0xc440100328
    go-consume/modules.(*ConfigOne).String(0xc42abcb4e0, 0x70bc08, 0xc42abd6300)
    :1 +0x64 fp=0xc4401003d8 sp=0xc440100398
    fmt.(*pp).handleMethods(0xc42abd6300, 0xc400000076, 0x410301)</p>
  </blockquote>
  
  <p>and finally, before dying, </p>
  
  <blockquote>
    <p>modules/structs.go:31 +0xc0 fp=0xc440103d18 sp=0xc440103ca8
    ...additional frames elided...</p>
    
    <p>goroutine 17 [syscall, locked to thread]:
    runtime.goexit()</p>
  </blockquote>
  
  <p>which I believe is caused by going into some infinite recursion. </p>
  
  <p>I tried my luck to find the cause and reached <a href="https://github.com/davecgh/go-spew/issues/45" rel="noreferrer">here</a>, which I believe is the same issue. However I couldn't understand the explanation in that thread.</p>
  
  <p>If I try to print the individual nested struct as</p>
  
  <pre><code>func (c ConfigOne)String() string{
      //return fmt.Sprintf("%+v\n", c.Daemon.Loglevel)
      return fmt.Sprintf("%+v\n", c.Daemon)
  }
  </code></pre>
  
  <p>it is working fine, and log shows the fields as</p>
  
  <pre><code>2017/03/05 01:28:25 go-consume.go:38: INFO: {Loglevel:1 Logfile:/tmp/test.log}
  </code></pre>
  
  <p>Can someone kindly explain how the former <code>String()</code> method is resulting in an infinite recursion and a stackoverflow, and what is the best way to overcome this?</p>