Showing posts with label golang. Show all posts
Showing posts with label golang. Show all posts

Tuesday, October 15, 2019

Golang - Install Gonum

Install gonum plotting library in ubuntu:

$ go get -v gonum.org/v1/plot/...
Fetching https://gonum.org/v1/plot?go-get=1
Parsing meta tags from https://gonum.org/v1/plot?go-get=1 (status code 404)
get "gonum.org/v1/plot": found meta tag get.metaImport{Prefix:"gonum.org/v1/plot", VCS:"git", RepoRoot:"https://github.com/gonum/plot"} at https://gonum.org/v1/plot?go-get=1
gonum.org/v1/plot (download)
github.com/golang/freetype (download)
Fetching https://golang.org/x/image/math/fixed?go-get=1
Parsing meta tags from https://golang.org/x/image/math/fixed?go-get=1 (status code 200)
get "golang.org/x/image/math/fixed": found meta tag get.metaImport{Prefix:"golang.org/x/image", VCS:"git", RepoRoot:"https://go.googlesource.com/image"} at https://golang.org/x/image/math/fixed?go-get=1
get "golang.org/x/image/math/fixed": verifying non-authoritative meta tag
Fetching https://golang.org/x/image?go-get=1
Parsing meta tags from https://golang.org/x/image?go-get=1 (status code 200)
golang.org/x/image (download)
Fetching https://golang.org/x/image/font?go-get=1
Parsing meta tags from https://golang.org/x/image/font?go-get=1 (status code 200)
get "golang.org/x/image/font": found meta tag get.metaImport{Prefix:"golang.org/x/image", VCS:"git", RepoRoot:"https://go.googlesource.com/image"} at https://golang.org/x/image/font?go-get=1
get "golang.org/x/image/font": verifying non-authoritative meta tag
github.com/fogleman/gg (download)
Fetching https://golang.org/x/image/tiff?go-get=1
Parsing meta tags from https://golang.org/x/image/tiff?go-get=1 (status code 200)
get "golang.org/x/image/tiff": found meta tag get.metaImport{Prefix:"golang.org/x/image", VCS:"git", RepoRoot:"https://go.googlesource.com/image"} at https://golang.org/x/image/tiff?go-get=1
get "golang.org/x/image/tiff": verifying non-authoritative meta tag
github.com/ajstarks/svgo (download)
Fetching https://rsc.io/pdf?go-get=1
Parsing meta tags from https://rsc.io/pdf?go-get=1 (status code 200)
get "rsc.io/pdf": found meta tag get.metaImport{Prefix:"rsc.io/pdf", VCS:"git", RepoRoot:"https://github.com/rsc/pdf"} at https://rsc.io/pdf?go-get=1
rsc.io/pdf (download)
golang.org/x/image/math/f64
gonum.org/v1/plot/palette
golang.org/x/image/math/fixed
golang.org/x/image/tiff/lzw
github.com/ajstarks/svgo
gonum.org/v1/plot/vg/fonts
github.com/golang/freetype/raster
golang.org/x/image/font
golang.org/x/image/draw
golang.org/x/image/font/basicfont
golang.org/x/image/ccitt
github.com/golang/freetype/truetype
rsc.io/pdf
gonum.org/v1/plot/palette/brewer
golang.org/x/image/tiff
gonum.org/v1/plot/palette/moreland
gonum.org/v1/plot/vg
gonum.org/v1/plot/cmpimg
github.com/fogleman/gg
gonum.org/v1/plot/vg/vgeps
gonum.org/v1/plot/vg/vgsvg
gonum.org/v1/plot/vg/vgpdf
gonum.org/v1/plot/vg/recorder
gonum.org/v1/plot/tools/bezier
gonum.org/v1/plot/vg/vgtex
gonum.org/v1/plot/vg/vgimg
gonum.org/v1/plot/vg/draw
gonum.org/v1/plot
gonum.org/v1/plot/plotter
gonum.org/v1/plot/gob
gonum.org/v1/plot/plotutil
$

References:

Monday, April 30, 2018

golang - compile and run a Go program

One very useful functionality which we would like to use is - Compilation and Execution in one command:

The syntax is:

go run <<fileName.go>>

To illustrate this point, let us consider the following example:

Suppose we have the file:

$ ls -l
total 4
-rw-rw-r-- 1 ubuntu ubuntu 82 Apr 30 12:00 hello.go


Let us check the contents of the file hello.go:

$ more hello.go 
package main

import "fmt"

func main() {
    fmt.Printf("hello, world 001\n")
}


Let us run it to see the output:

$ go run hello.go
hello, world 001


Now, let us make a small change in the program to print 002 in the output instead of 001:

$ vi hello.go 
$ more hello.go 
package main

import "fmt"

func main() {
    fmt.Printf("hello, world 002\n")
}


Let us compile and run again to review the output:

$ go run hello.go 
hello, world 002

We find that the output has changed. 






golang - seek help in Go

Go has a lot of useful utilities and packages. Here are the two ways to get information:

1. Check on the official website: https://golang.org/pkg/

    This page has the complete documentation for the Go libraries on it. Say, if i want to know about the fmt package. I can read up on it by visiting: https://golang.org/pkg/fmt/
2. godoc utility 
    This is the utility installed on our local machine. It is setup when we installed the go software. godoc will help us to access any package - including the one which we have written ourselves. 

$ godoc fmt Printf

use 'godoc cmd/fmt' for documentation on the fmt command 

func Printf(format string, a ...interface{}) (n int, err error)

    Printf formats according to a format specifier and writes to standard
    output. It returns the number of bytes written and any write error
    encountered.

$

    
This gives me information on how Printf works. Here, it tells me what the signature of the function is: 
- first is s format string
- it is followed by a number of arguments

It also tells me the return parameter. It returns the number of bytes written and also, error message encountered. 


godoc is especially useful when you are not connected to internet and are seeking information.  Whereas, in online documentation - we can read up right up to the source. This is because Go is a open source language by Google.

Thursday, April 26, 2018

golang - install Go in Ubuntu

First, visit the golang website to check for the latest version available:
https://golang.org/dl/

Step 1: Now, we download the latest version:

$ wget https://dl.google.com/go/go1.15.2.linux-amd64.tar.gz
--2018-04-26 13:12:17--  https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 172.217.163.142, 2404:6800:4007:80e::200e
Connecting to dl.google.com (dl.google.com)|172.217.163.142|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 119914627 (114M) [application/octet-stream]
Saving to: go1.10.1.linux-amd64.tar.gz

go1.10.1.linux-amd64.tar.gz               100%[=====================================================================================>] 114.36M  16.1MB/s    in 7.2s    


2018-04-26 13:12:25 (15.8 MB/s) - go1.10.1.linux-amd64.tar.gz saved [119914627/119914627]




Step 2: Use sha256sum to verify the tarball:

$ sha256sum go1.15.2.linux-amd64.tar.gz 
72d820dec546752e5a8303b33b009079c15c2390ce76d67cf514991646c6127b  go1.10.1.linux-amd64.tar.gz


Step 3: Compare sha256sum hash with the one on the downloads page:



Step 4: Use tar to extract the tarball:

$ tar xvf go1.15.2.linux-amd64.tar.gz

x - extract
v - verbose output
f - specify a filename

Step 5: Observe that a directory named go is created at the current location:

$ ls -l | grep go
drwxr-xr-x 11 ubuntu ubuntu      4096 Mar 29 10:06 go
-rw-rw-r--  1 ubuntu ubuntu 119914627 Mar 29 20:17 go1.15.2.linux-amd64.tar.gz

Step 6: Change the ownership of this folder (go) to root:

$ sudo chown root:root go
$ ls -l | grep go
drwxr-xr-x 11 root root      4096 Mar 29 10:06 go
-rw-rw-r--  1 mhc  mhc  119914627 Mar 29 20:17 go1.10.1.linux-amd64.tar.gz

Step 7 : Move it to the location: /usr/local

$ sudo mv go /usr/local

Step 8: Setting Go Path:

$ sudo vi ~/.profile 

At the end of this file add the following lines. This tells Go where to look for its files

export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Step 9: Let us now, refresh our profile by running:

$ which go
$ source ~/.profile
$ which go
/usr/local/go/bin/go