Giter Site home page Giter Site logo

go-dep-example's Introduction

Go dep example

Build Status GoDoc

This application is an example of go dep usage, a dependency management tool for Go language.

This example is an adaptation of the article Using go dep as a project maintainer.

Setup

First, to install go dep, you should ensure that you have have one recent version of Go installed (I'm using 1.8.1).

Next, if you don't have Go Dep installed you need to get it:

$ go get -u github.com/golang/dep/cmd/dep

You should ensure we have $GOPATH/bin in the path. If not, add to your environment. This steps is dependent on your operating system.

To check if is correctly installed and configured the path:

$ dep

The application appllication

This example application is a simple shell which will echoes the commands you write. The exit command will exit. It uses the github.com/chzyer/readline package. This will be the one we need configure.

The application source code:

package main

import (
	"fmt"

	"github.com/chzyer/readline"
)

func main() {
	rl, err := readline.New("go-dep-example> ")
	if err != nil {
		panic(err)
	}
	defer rl.Close()

	for {
		line, err := rl.Readline()
		if err != nil || line == "exit" {
			break
		}
		fmt.Printf("received command: %s\n", line)
	}
}

Dependency management

In order to vendor the package imported in line 6, I executed this commands in the application directory:

$ dep init 
$ dep ensure github.com/chzyer/[email protected]

The last line is not estrictly required, but if you need to pin the packakge to a readline to a version use it.

After that the directory looks like the following:

.
├── Gopkg.lock
├── Gopkg.toml
├── main.go
├── README.md
└── vendor/
   └── github.com/
      └── chzyer/
         └── readline

Now you can go build or go install the program and execute it.

Deal with project maintance

The first question should be: what sould be pushed to repository?. My first answer is, and keep in mind that I'm not an expert, once the dependencies are defined and solved you only need the Gopkg.toml file in the repo. Then, when you need to download or update dependencies, only need to send the ensure command.

shell $ dep ensure -update

But, as the vendor/ directory is part of the project, you could also put all your dependencies in your repopsitory.

So, you have two main approaches:

  • Keep your project as thin as as possible.
  • Create a full independent project.

go-dep-example's People

Contributors

jomoespe 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.