Custom Search
Showing posts with label go. Show all posts
Showing posts with label go. Show all posts

Saturday, September 16, 2017

How to Setup Golang Go Development Environment with Atom Editor IDE

1)
Setup the "GOPATH" for your Go Projects
vim ~/.bashrc

export GOROOT=$HOME/go
export PATH=$GOROOT/bin:$PATH

export GOPATH=$HOME/mygoproject1
export PATH=$GOPATH/bin:$PATH

===========


1)
sudo add-apt-repository ppa:webupd8team/atom

2)
sudo apt update

3)
Install
sudo apt install atom

4)
Remove
sudo apt remove --purge atom

============

1)
Launch Atom.
Go to "Edit" ---> "Preferences".
Click on +Install

In the Search packages search box, enter the following plugins and install it:

a) go-plus
b) atom-runner

3)
Close Atom Editor and launch it again.


4)
In the Atom Editor, go to "File" ---> "Add Project Folder".
This will bring up a Folder selection dialog and you can point it to the root of your application.

5)
Create a new file named main.go and create "Hello World" Program.

6)
Check auto-complete, Try "Ctrl-Space".

7)
Select *.go file and press "CTRL+SHIFT+P", then type "run file" and enter to run the go file.

Sunday, July 9, 2017

golang How to Install and Setup Jupyter notebook

1)
Download Go compiler from https://golang.org/dl/

2)
Extract it ($HOME/gocompiler/)

3)
Create a new directory for your go project ($HOME/golexamples/myproject1)

4)
Add the following lines to your $HOME/.bashrc

export GOROOT=$HOME/gocompiler/go
export PATH=$GOROOT/bin:$PATH

export GOPATH=$HOME/golexamples/myproject1
export PATH=$GOPATH/bin:$PATH



5)
$go help

6)
#we need pip >= 9
pip —version

7)
sudo pip install —upgrade pip

8)
pip —version

i)
sudo pip install jupyter

9)
#Run server
jupyter notebook

10)
Install goimports
go get golang.org/x/tools/cmd/goimports

11)
sudo apt-get install libzmq3-dev

12)
Install golang kernel
go get -tags zmq_4_x github.com/gopherds/gophernotes

Ref:
https://github.com/gopherdata/gophernotes

13)
Create a directory for the new kernel config:
mkdir -p ~/.local/share/jupyter/kernels/gophernotes

14)
Copy the kernel config into the .ipython directory:
cp -r $GOPATH/src/github.com/gopherds/gophernotes/kernel/* ~/.local/share/jupyter/kernels/gophernotes

15)
Start the jupyter notebook:
jupyter notebook

16)
Then, select “Golang” from the “New” drop down menu

17)
import "fmt"
world := "world"
fmt.Sprintf("Hello %s", world)





Wednesday, May 17, 2017

Golang Go no new variables on left side of :=


$vim test.go

package main

import "fmt"

func main() {
    //var age int = 42
    //age := 42
    var age int
    age := 42
    fmt.Printf("%#v\n", age)
}




OutPut:
======

$go run test.go
# command-line-arguments
./test.go:8: no new variables on left side of :=
$

Golang Go Primitive data types

package main

import "fmt"

//int, uint, int8, uint8
//string
//float32, float64
//bool
//complex64, complex128



func main(){

    fmt.Printf("Value: %v, Type: %T\n", "Hello", "Hello")
    fmt.Printf("Value: %v, Type: %T\n", 8, 8)
    fmt.Printf("Value: %v, Type: %T\n", uint(9), uint(9))
    fmt.Printf("Value: %v, Type: %T\n", int8(6), int8(6))
    fmt.Printf("Value: %v, Type: %T\n", true, true)
    fmt.Printf("Value: %v, Type: %T\n", 4.1, 4.1)
    fmt.Printf("Value: %v, Type: %T\n", (1 + 5i), (1 + 5i))
}

OutPut:
=========
$go run primitive_types.go

Value: Hello, Type: string
Value: 8, Type: int
Value: 9, Type: uint
Value: 6, Type: int8
Value: true, Type: bool
Value: 4.1, Type: float64
Value: (1+5i), Type: complex128
$
 

Tuesday, May 16, 2017

Golang Go Programming Setup GOPATH and install package

a)
Create workspace directory structure
$mkdir mygoproject
$cd mygoproject
$mkdir bin
$mkdir src
$mkdir pkg

b)
Export path of workspace and bin directory.
$export GOPATH=$HOME/golang_tuto/mygoproject
$export PATH=$GOPATH/bin:$PATH



c)
Install dependency packages in workspace:
$go get github.com/gorilla/mux
$go get -u github.com/gorilla/mux

d)
Then check bin, src and pkg directories fo workspace.
$ls mygoproject/bin
$ls mygoproject/src
$ls mygoproject/pkg

go golang how to remove packages installed with go get

a)
Install package
$go get -x github.com/gorilla/mux



b)
Remove package
$go clean -i -x github.com/gorilla/mux

golang go how to install package from Github

a)
Create workspace directory structure

$mkdir myproject
$cd myproject
$mkdir bin
$mkdir src
$mkdir pkg

b)
Export path of workspace and bin directory.

$export GOPATH=$HOME/golang_tuto/myproject
$export PATH=$GOPATH/bin:$PATH






c)
Install dependency packages in workspace:

$go get github.com/gorilla/mux
$go get -u github.com/gorilla/mux

$go get golang.org/x/crypto/bcrypt
$go get -u golang.org/x/crypto/bcrypt

$go get golang.org/x/tools/cmd/goimports

d)
Then check bin, src and pkg directories of workspace.

$ls myproject/bin
$ls myproject/src
$ls myproject/pkg

How does the golang go build command work

$go build --help
$go build -x hello.go
$go build -x -work hello.go



$go build -x -a hello.go
$go build -x -n hello.go

go golang How to install dependency packages in workspace

a)
Create workspace directory structure
$mkdir mygoproject
$cd mygoproject
$mkdir bin
$mkdir src
$mkdir pkg

b)
Export path of workspace and bin directory.
$export GOPATH=$HOME/golang_tuto/mygoproject
$export PATH=$GOPATH/bin:$PATH



c)
Install dependency packages in workspace:
$go get github.com/gorilla/mux
$go get -u golang.org/x/crypto/bcrypt
$go get golang.org/x/tools/cmd/goimports

d)
Then check bin, src and pkg directories fo workspace.
$ls mygoproject/bin
$ls mygoproject/src
$ls mygoproject/pkg



go Golang How to create workspace for a project

a)
Create workspace directory structure
$mkdir mygoproject
$cd mygoproject
$mkdir bin
$mkdir src
$mkdir pkg



b)
Export path of workspace and bin directory.
$export GOPATH=$HOME/golang_tuto/mygoproject
$export PATH=$GOPATH/bin:$PATH


c)
Test workspace by installing a package from github:

$go get github.com/gorilla/mux
 

How to install latest Go Compiler on GNU/Linux

a)
Download Go compiler from https://golang.org/dl/

b)
Extract it ($HOME/golang_tuto/)

c)
Create a new directory for your go project ($HOME/golang_tuto/mygo)



d)
Add the following lines to your $HOME/.bashrc
export GOROOT=$HOME/golang_tuto/go
export PATH=$GOROOT/bin:$PATH

export GOPATH=$HOME/golang_tuto/mygo
export PATH=$GOPATH/bin:$PATH

e)
$go help


f)
$vim hello.go

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

g)
$go build hello.go
$./hello

OR

$go run hello.go

Tuesday, October 25, 2016

Go Golang decode unmarshal json string to map

package main

import (
"encoding/json"
"fmt"
"reflect"
)



func main() {

str := `{"name": "sam",
             "age": 30,
             "details": {"salary":10000}
            }`

/*This means the key of map named data is a string but the value can be anything*/
/*An interface{} type is a type that could be any value. It’s like Object in Java.*/
var data map[string]interface{}

fmt.Println("==type of str==", reflect.TypeOf(str))
fmt.Println("==str==", str)

/*json.Unmarshal is used when the input is []byte*/
/*stores the decoded result in map named data*/
fmt.Println("==[]byte(str)==", []byte(str))
err := json.Unmarshal([]byte(str), &data)

if err != nil {
panic(err)
}

fmt.Printf("==data==%#v \n\n", data)
fmt.Println("==name== ", data["name"])

/*Type assertions, check type of data["details"]
 https://tour.golang.org/methods/15*/
details, ok := data["details"].(map[string]interface{})
if ok {
fmt.Println("==salary== ", details["salary"])
}
}

Thursday, September 15, 2016

Go Golang Pretty Print struct, map, slice, array

package main

/* https://golang.org/pkg/fmt/ */
import (
"encoding/json"
"fmt"
)

/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
Name    string
Age     int
Hobbies []string
}




func main() {

/*initialize a struct.
 Create an instance of type 'Person'*/
p1 := &Person{"Sam", 20, []string{"cricket", "football"}}
/*p1 := &Person{name: "Sam", age: 20, hobbies: []string{"cricket", "football"}}*/

p1_json, _ := json.Marshal(p1)
fmt.Println("==p1_json==", string(p1_json))

p1_json_ind, _ := json.MarshalIndent(p1, "", "  ")
fmt.Println("==p1_json_ind==", string(p1_json_ind))

/*With type and field name*/
/* %#v a Go-syntax representation of the value*/
fmt.Printf("==p1==%#v \n", p1)

/*MAP*/
mapD := map[string]int{"apple": 5, "lettuce": 7}
mapB, _ := json.MarshalIndent(mapD, "", " ")
fmt.Println("==mapD==", string(mapB))
fmt.Printf("==map==%#v \n", mapD)

/*SLICE*/
/*https://blog.golang.org/go-slices-usage-and-internals
 https://blog.golang.org/slices*/
slcD := []string{"apple", "peach", "pear"}
slcB, _ := json.MarshalIndent(slcD, "", " ")
fmt.Println("==slice==", string(slcB))
fmt.Printf("==slice=%#v \n", slcB)

}

Sunday, August 21, 2016

Go Golang decode unmarshal json string to struct

package main

import (
"encoding/json"
"fmt"
"reflect"
)

type Person struct {
Name    string
Age     int
Details interface{}
}



func main() {

str := `{"name": "sam",
             "age": 30,
             "details": {"salary":10000}
            }`

/*This means the key of map named data is a string but the value can be anything*/
/*An interface{} type is a type that could be any value. It’s like Object in Java.*/
var data Person

fmt.Println("==type of str==", reflect.TypeOf(str))
fmt.Println("==str==", str)

/*json.Unmarshal is used when the input is []byte*/
/*stores the decoded result in map named data*/
fmt.Println("==[]byte(str)==", []byte(str))
err := json.Unmarshal([]byte(str), &data)

if err != nil {
panic(err)
}

fmt.Printf("==data==%#v \n\n", data)
fmt.Println("==name== ", data.Name)

/*Type assertions, check type of data["details"]
 https://tour.golang.org/methods/15*/
details, ok := data.Details.(map[string]interface{})
if ok {
fmt.Println("==salary== ", details["salary"])
}
}

Go Golang Lowercase key names with JSON Marshal and UnMarshal

package main

/* https://golang.org/pkg/fmt/ */
import (
"encoding/json"
"fmt"
)



/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
Name    string   `json:"name"`
Age     int      `json:"age"`
Hobbies []string `json:"hobbies"`
}

func main() {

/*initialize a struct.
 Create an instance of type 'Person'*/
p1 := Person{"Sam", 20, []string{"cricket", "football"}}
/*p1 := &Person{name: "Sam", age: 20, hobbies: []string{"cricket", "football"}}*/

/*ENCODE*/
/*p1_json_ind, _ := json.MarshalIndent(p1, "", "  ")*/
p1_json, _ := json.Marshal(p1)
fmt.Println("==p1_json==", string(p1_json))

/*DECODE*/
var p2 Person
decode_error := json.Unmarshal(p1_json, &p2)
fmt.Printf("==decode_error==%v \n", decode_error)

fmt.Printf("==p2==%#v \n", p2)

}

Go Golang Pretty Print struct, map, slice, array json.MarshalIndent

package main

/* https://golang.org/pkg/fmt/ */
import (
"encoding/json"
"fmt"
)

/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
Name    string
Age     int
Hobbies []string
}



func main() {

/*initialize a struct.
 Create an instance of type 'Person'*/
p1 := &Person{"Sam", 20, []string{"cricket", "football"}}
/*p1 := &Person{name: "Sam", age: 20, hobbies: []string{"cricket", "football"}}*/

p1_json_ind, _ := json.MarshalIndent(p1, "", "  ")
fmt.Println("==p1_json_ind==", string(p1_json_ind))

/*MAP*/
mapD := map[string]int{"apple": 5, "lettuce": 7}
mapB, _ := json.MarshalIndent(mapD, "", " ")
fmt.Println("==mapD==", string(mapB))

/*SLICE*/
/*https://blog.golang.org/go-slices-usage-and-internals
 https://blog.golang.org/slices*/
slcD := []string{"apple", "peach", "pear"}
slcB, _ := json.MarshalIndent(slcD, "", " ")
fmt.Println("==slice==", string(slcB))
}

Go Golang json.Marshal returns {} when struct field start with lowercase

package main

/* https://golang.org/pkg/fmt/ */
import (
"encoding/json"
"fmt"
)



/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
Name    string
Age     int
Hobbies []string
}

func main() {

/*initialize a struc.
 Create an instance of type 'Person'*/
p1 := &Person{"Sam", 20, []string{"cricket", "football"}}
/*p1 := &Person{name: "Sam", age: 20, hobbies: []string{"cricket", "football"}}*/

p1_json, _ := json.Marshal(p1)
fmt.Println("===p1_json===", string(p1_json))

p1_json_ind, _ := json.MarshalIndent(p1, "", "  ")
fmt.Println("===p1_json_ind===", string(p1_json_ind))

/*With type and field name*/
/* %#v a Go-syntax representation of the value*/
fmt.Printf("=======%#v \n", p1)

}

Friday, August 19, 2016

Go Golang How to pretty print struct variable

1)
package main

/* https://golang.org/pkg/fmt/ */
import (
"fmt"
)

/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
name string
age  int
}

func main() {

/*initialize a struc.
 Create an instance of type 'Person'*/
p := Person{"Sam", 20}

fmt.Println("====", p)

/* %v  print the value in a default format*/
fmt.Printf("=====%v \n", p)

/*With field name*/
fmt.Printf("======%+v \n", p)

/*With type and field name*/
/* %#v a Go-syntax representation of the value*/
fmt.Printf("=======%#v \n", p)
}



2)
output
----------
$go run print_struct.go
==== {Sam 20}
====={Sam 20} 
======{name:Sam age:20} 
=======main.Person{name:"Sam", age:20} 

How to use Sublime Text 2 for Go Golang Development

1)
Goto
"Tools" == "Build System" == "New Build System"

2)
Save the file with name "go.sublime-build"
{
    "cmd": "go run $file",
    "shell" : true
}

* this file will save under /home/your-name/.config/sublime-text-2/Packages/User

3)
Goto
"Tools" == "Build System" == "New Build System"
Then select "go"

4)
Create simple go program in sublime text and select
"Tools" == "Build"