• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer

GitopsCentral

  • Home
  • Courses
  • Roadmap
  • About
  • Log In
  • Sign Up

Go Modules

May 21, 2019 by shaik zillani

© cloudinary

According to golang official github,
Modules are an experimental opt-in feature in Go 1.11, with the plan of incorporating feedback and finalizing the feature for Go 1.13.

The initial prototype (vgo) was announced in February 2018. In July 2018, support for versioned modules landed in the main repository.

Example

Create a directory called test and execute the below commands,

go mod init test

Inside the directory create files root.go and main.go
Below is an example take from cobra project.

main.go

package main

import (
  "{pathToYourApp}/cmd"
)

func main() {
  cmd.Execute()
}

root.go

package cmd

import (
    "fmt"
    "github.com/spf13/cobra"
    "os"
)

var rootCmd = &cobra.Command {
    Use: "hugo",
    Short: "Hugo is a very fast static site generator",
    Long: `A Fast and Flexible Static Site Generator built with
love by spf13 and friends in Go.
Complete documentation is available at http://hugo.spf13.com`,
    Run: func(cmd * cobra.Command, args[] string) {
        fmt.Println("this is cobra hello!")
    },
}

func Execute() {
    if err: = rootCmd.Execute();
    err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

Execute

go build 
./test
this is cobra hello

The purpose of this article is to show how to create a go-module project and execute sample go files. Using go modules we can create our projects outside the $GOPATH.

Just a note: If you using an IDE, go with default go settings as show below, make sure you check the “index entire GOPATH”

golang go,  go modules,  golang

Primary Sidebar

en English
ar Arabiczh-CN Chinese (Simplified)nl Dutchen Englishfr Frenchde Germanhi Hindiid Indonesianit Italianja Japanesems Malaypt Portugueseru Russianes Spanishte Telugu

Course Tags

concurrency free go golang

Recent Posts

  • Roadmap to Mastery
  • Increase SSH connection timeout
  • Check Certificate expiry in Kubernetes
  • space invaders in golang
  • creating kubernetes cluster using eksctl
  • Facebook
  • GitHub
  • YouTube

Contact

Navigation

  • goacademy pro

Footer

  • Facebook
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter

info@gitopscentral.com


+91 8341443220

© Copyright 2016-2024 gitopscentral · All Rights Reserved ·