title: How to write mock for structs in Go
body: <p>I want to write a unit test for the <code>Transport</code> function which will require mocking <code>CarFactory</code> and <code>Car</code> structs. See the following code:</p>
  
  <pre><code>package main
  
  type Car struct {
      Name string
  }
  
  func (h Car) Run() { ... }
  
  type CarFactory struct {}
  
  func (e CarFactory) MakeCar() Car {
      return Car{}
  }
  
  func Transport(cf CarFactory) {
      ...
      car := cf.MakeCar()
      car.Run()
      ...
  }
  </code></pre>
  
  <p>In other OOP languages like Java, C# or C++, I can just define <code>CarFactoryMock</code> and <code>CarMock</code> that extend <code>CarFactory</code> and <code>Car</code> then override <code>MakeCar()</code> method to return a <code>CarMock</code> object</p>
  
  <pre><code>class CarMock extends Car {
      public Run() {...}
  }
  
  class CarFactoryMock extends CarFactory {
      public Car MakeCar() { return new CarMock(); }                                                                                                                                                                                        
  }
  
  Transport(new CarFactoryMock())
  </code></pre>
  
  <p>How do I achieve this in Go?</p>
  
  <p><strong>Note that I can change prototype and source code of <code>Transport</code> function, but must keep <code>CarFactory</code> and <code>Car</code> the same since they are taken from a 3rd package</strong></p>
  
  <hr>
  
  <p>The last code snippet was about Human and Employee, which lead to confusion`.</p>