title: Define method of struct inside function
body: <p>I have this code</p>
  
  <pre><code>func baz() {
      type mockDatabase struct{}
  
      //
      //  More code
      //
  }
  </code></pre>
  
  <p>I want to define a method of <code>mockDatabase</code>, so the full code would look like this:</p>
  
  <pre><code>func baz() {
      type mockDatabase struct{}
  
      func (m *mockDatabase) Foo() {
          // Implement function here
      }
  
      //
      //  More code
      //
  }
  </code></pre>
  
  <p>The reason I want to do this is I'm injecting a dependency into a function, and I want to create a "mock object" to inject into the function (the function takes an interface as an argument and the mock object will implement the interface).</p>
  
  <p>I could create the struct outside, but it seems more logical to declare the struct locally to decrease namespace clutter, especially when these mock objects will only be used once. Am I missing something here? Is it better practice to define it outside the test function so it's not long? What should I do here?</p>