Write Hello World Application using Glide

  1. Create a folder under $GOPATH/src/ like this and go to that directory:
    $ cd $GOPATH/src/
    $ mkdir -p github.com/gotutorial/helloworld/helloworld-glide 
    $ cd github.com/gotutorial/helloworld/helloworld-glide
  2. Initiate a glide yaml file called glide.yaml inside that directory using an editor.
    $ vi glide.yaml

    And add following line into it:

    package: github.com/gotutorial/helloworld/helloworld-glide
    import:
    - package: github.com/dustin/go-humanize

    The first line package: github.com/gotutorial/helloworld is defining your project package so that if another project is going to have dependency to your project or library they will use this package path there.

    The second and third lines is related to your imported dependencies. I am using a library called go-humanize to format numbers is human readable format like 1024 (bytes) can be displayed as 1MB. 

  3. The next step is creating your application starting point file. Usually I name it main.go, but you can name it whatever you like. I am fan of vi CLI editor tool to create and modify files.
    $ vi main.go

    Add following codes into main.go file:

    package main
    
    import humanize "github.com/dustin/go-humanize"
    import "fmt"
    
    func main() {
      fmt.Println("Hello World !!")
      fmt.Printf("Sample File Size in MB: %s.\n", humanize.Bytes(48234496)) // Sample File Size in MB: 46MB
    }
  4. Using glide install you can install your project dependencies. Technically, it installs the dependencies into a folder beside glide.yaml file called vendor 
    $ glide install

    You should be able to see following lines:

    [INFO]	Lock file (glide.lock) does not exist. Performing update.
    [INFO]	Downloading dependencies. Please wait...
    [INFO]	--> Fetching updates for github.com/dustin/go-humanize
    [INFO]	Resolving imports
    [INFO]	Downloading dependencies. Please wait...
    [INFO]	Setting references for remaining imports
    [INFO]	Exporting resolved dependencies...
    [INFO]	--> Exporting github.com/dustin/go-humanize
    [INFO]	Replacing existing vendor dependencies
    [INFO]	Project relies on 1 dependencies.

     

  5. If you list the files on the root directory of your project, you should be able to find a file called glide.lock and a folder called vendor which has your project dependencies files

    $ ls
    glide.lock  glide.yaml  main.go  vendor

     Go inside the vendor folder and list the files using ls command to see how glide install downloads and stores the dependencies you are referring in import tag inside glide.yaml file

    $ cd vendor
    $ ls
    github.com
    $ cd github.com
    $ ls
    dustin
    $ cd dustin
    $ ls
    go-humanize
    $ cd go-humanize
    $ ls
    LICENSE          bigbytes.go       bytes_test.go  commaf.go       english       humanize.go     ordinals.go       si_test.go
    README.markdown  bigbytes_test.go  comma.go       commaf_test.go  ftoa.go       number.go       ordinals_test.go  times.go
    big.go           bytes.go          comma_test.go  common_test.go  ftoa_test.go  number_test.go  si.go             times_test.go
    
    

    Go back to your project root

    $ cd ../../../../
  6. Now you can run following command to build your app. You should be able to find an executable file named myapp in your project root directory
    $ go build -o myapp
    
    glide.lock  glide.yaml  main.go  myapp  vendor
  7. Finally, run myapp like this. You should be able to see ‘Hello World !!’ prompt in the first line and ‘Sample File Size in MB: 48 MB.’ in the second line
    $ ./myapp
    Hello World !!
    Sample File Size in MB: 48 MB.

     

    You can find the source code in my github here:
    https://github.com/gotutorial/helloworld.git

Leave a Reply