Showing posts with label Golang. Show all posts
Showing posts with label Golang. Show all posts

Thursday, September 8, 2016

Golang and Shell Script : Execute sh file from Go file and vice-versa.

We will see here today, executing a Go file from Shell script file and vice-versa.

Don't know, how to create and run a shell script file ?
Shell script file name : hello_sh.sh
Content of hello_sh.sh :

echo "Hello, I am inside 'hello_sh.sh' file."
echo "Now, I am going to call 'hello_go.go' file. The output: "
go run hello_go.go


Hello world ! program in Golang.
Go file name : hello_go.go
Content of hello_go.go :


package main
import (
        "bytes"
        "fmt"
        "log"
        "os/exec"
        "strings"
)
func main() {
        fmt.Printf("Hello, I am from 'hello_go.go' file.");
        fmt.Printf("\nNow, I am going to execute 'hello_sh.sh' file\n");
        cmd := exec.Command("bash", "hello_sh.sh");
        cmd.Stdin = strings.NewReader("");
        var out bytes.Buffer;
        cmd.Stdout = &out;
        err := cmd.Run();
        if err != nil {
                log.Fatal(err);
        }
        fmt.Printf("Output \n",out.String());
}


*For beginner change the file name "hello_sh.sh" and  "hello_go.go" according to your requirement.
*For advanced programmer, try this recursion and you can comment your output below.


-Thank you




Wednesday, May 20, 2015

GO Programming Language : Executing external system command using "Go" language

Prerequisite : you must have basic knowledge of how to execute a programming in Go language.
How execute first hello world program.

In following section, we will discuss, how to execute an external program. My goal is, instead of executing a command in terminal, I want to execute using Go programming language.

Source Code:
package main
import (
        "bytes"
        "fmt"
        "log"
        "os/exec"
        "strings"
)

func main() {
        cmd := exec.Command("docker","ps","-a")
        cmd.Stdin = strings.NewReader("")
        var out bytes.Buffer
        cmd.Stdout = &out
        err := cmd.Run()
        if err != nil {
                log.Fatal(err)
        }
        fmt.Printf("Output \n",out.String())
}

Output:
Ahh... I got a long list of output as expected.


Description:
In the source, I was trying to execute the following command
docker ps -a
For this, I pass the above command as argument to function "command" as different strings.
When I tried to pass whole command (i.e. docker ps -a) as a single string, it produce an error saying "exec: "docker ps -a": executable file not found in $PATH exit status 1". This means the first argument is the name of the executable file and in order to execute the command it tries to search the file mentioned in first string in function "exec.Command()". Since in my case the actual executable file name is "docker", this couldn't execute the "docker ps -a" command.
So, every word in the command (or the external program we want to execute) must be provided as different string to "exec.Command()".

That's all for now.


-Enjoy


 

GO Programming Language : First day with GO - Executing "Hello world" program.

OS : Ubuntu 12.04


Command to install
sudo apt-get install gccgo-go

Now type "go version" to know the version of  Go.
here is my output
go version xgcc (Ubuntu 4.9.1-0ubuntu1) 4.9.1 linux/amd64

Now its time to move on to our first hello world program.
Use any editor to create a Go file. Here I am using "vim" editor. Now create a Go file using following command. Depending upon the editor you are using following may differ.
vim hello.go

Now press i to enter INSERT mode and copy the following content

package main
import "fmt"

func main(){
   fmt.Printf("Hello, World\n");
}



Now press ":wq" to save and exit from editor.
Enter following command to run and see the output
go run hello.go

output:




That's all for now.


-Enjoy