Coder Social home page Coder Social logo

go-interface-examples's Introduction

go-interface-examples

Some examples of using Go interfaces to make cleaner, more testable code.

These assume general familiarity with Go syntax and the ability to write simple programs. If you haven't already, take a tour of Go! You'll be introduced to interfaces there. You should also read Effective Go.

The point of this repository is to expand on those ideas and explore them with simple but relatable examples to explain why Go tells you to use interfaces the way it does.

The Outside World

What are interfaces? What problems do interfaces solve? Why do I care?

My code is hard to test because it has to talk to a database! What do? What's this 'mock' thing I hear about?

Why should I pass in dependencies instead of just creating them or using globals?

Let's find out!

This is pretty entry level. We'll explore creating a simple service that ends up being easy to reason about and easy to test thanks to Go interfaces. We'll start as simple as possible and explore why we make each iteration of changes towards a more robust solution. This will be "interfaces the long way".

I think this is a good read if you're new to Go and/or relatively new to coding in general. You may find it useful even if you're intermediate, however, as I hope it articulates a lot of the underlying reasons behind doing the things you may already be doing with interfaces/IoC/DI.

Declare interfaces locally

Why does Go tell me to declare interfaces at the consumer level?

What's up with this tweet?

See the article here.

There's a reason libraries don't usually give you an interface to work with, and why you shouldn't provide one either. Let's explore why.

This is an example of defining your interfaces locally in each package rather than trying to do an "interface package" or providing giant interfaces from a single package that also contains the implementation.

I think this is a good read if you're coming to Go from another language and may have preconceived ideas of what interfaces should be because of that. You'll need to be at least a little familiar with Go at this point and may have written a service or two.

go-interface-examples's People

Contributors

evertras avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.