title: Understanding go tool compile and link commands
body: <p>I understand that there are three steps involved in converting a high level language code to machine language or executable code namely - compiling, assembling, and linking.</p>
  
  <p>As per go docs <strong>go tool compile</strong> does the following - 
  <code>It then writes a single object file named for the basename of the first source file with a .o suffix</code></p>
  
  <p>So, the final object file must contain the machine language code (after compiling and assembling are run) of each file. If I pass <strong>go tool compile -S</strong> on the go file, it shows assembly language go generates.</p>
  
  <p>After this when I run <strong>go tool link</strong> on the object file, it must link all required object files (if multiple) and then generate final machine language code (based on GOOS and GOARCH). It generates a file <strong>a.out</strong></p>
  
  <p>Have few basic questions here - </p>
  
  <p>How do I know which variables and when are they going to be allocated memory in stack and heap? Does it matter if I generate executable for one machine and run on another with different architecture?</p>
  
  <p><strong>My test program</strong></p>
  
  <pre><code>package main
  
  func f(a *int, b *int) *int { 
  
      var c = (*a + *b); 
      var d = c;
      return &amp;d;
  }
  
  func main() {
      var a = 2;
      var b = 6;
  
      f(&amp;a,&amp;b);
  }
  </code></pre>
  
  <p>Result of <strong>go tool compile -m -l test.go</strong></p>
  
  <pre><code>test.go:6: moved to heap: d
  test.go:7: &amp;d escapes to heap
  test.go:3: f a does not escape
  test.go:3: f b does not escape
  test.go:14: main &amp;a does not escape
  test.go:14: main &amp;b does not escape
  </code></pre>