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

Golang: Http POST Request with JSON Body example

Go standard library comes with "net/http" package which has excellent support for HTTP Client and Server.  
In order to post JSON body during post request, we need to convert the data to byte array format and send it along with the request.
You can convert the JSON to a byte array using "encoding/json" package. Then use the NewBuffer method to pass this byte array to the post method.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

//If the struct variable names does not match with json attributes 
//then you can define the json attributes actual name after json:attname as shown below. 
type User struct {
	Name string  	`json:"name"`
	Job string 	`json:"job"`
}

func main(){

	//Create user struct which need to post.
	user := User{
		Name: "Test User",
		Job: "Go lang Developer",
	}

	//Convert User to byte using Json.Marshal
	//Ignoring error to
	body, _ := json.Marshal(user)

	//Pass new buffer for request with URL to post.
	resp, err := http.Post("https://reqres.in/api/users", "application/json", bytes.NewBuffer(body) )

	// An error is returned if there were too many redirects
	// or if there was an HTTP protocol error
	if err != nil {
		panic(err)
	}
	//Need to close the response stream, once response is read.
	//Hence defer close. It will automatically take care of it.
	defer resp.Body.Close()

	//Check response code, if New user is created then read response.
	if resp.StatusCode == http.StatusCreated {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			//Failed to read response.
			panic(err)
		}

		//Convert bytes to String and print
		jsonStr := string(body)
		fmt.Println("Response: ", jsonStr)

	} else {
		//The status is not Created. print the error.
		fmt.Println("Get failed with error: ", resp.Status)
	}
}

Golang: Http Get Request example

Go standard library comes with "net/http" package which has excellent support for  HTTP Client and Server.  
In this example we will be using "http.Get" method to execute Http Get method. We will convert the response to String and print it.

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func main(){

	//Use get method to hit the rest API
	resp, err := http.Get("https://reqres.in/api/users/2")

	// An error is returned if there were too many redirects
	// or if there was an HTTP protocol error
	if err != nil {
		panic(err)
	}
	//Need to close the response stream, once response is read.
	//Hence defer close. It will automatically take care of it.
	defer resp.Body.Close()

	//Check response code, if ok then read response.
	if resp.StatusCode == http.StatusOK {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			//Failed to read response.
			panic(err)
		}

		//Convert bytes to String and print
		jsonStr := string(body)
		fmt.Println("Response: ", jsonStr)

	} else {
		//The status is not ok. print the error.
		fmt.Println("Get failed with error: ", resp.Status)
	}
}
Its simple and easy to implement.

Golang: Reading environment variables

Usually, environment variables are used to provide the configuration information for the program. You can set the environment outside of the program and you can assess these during execution. Go provides the simplest mechanism to access the environment variables. 
You can use 'OS' package to get or set the environment variables.

package main

import (
	"fmt"
	"os"
	"strings"
)

func main() {
	//Read single environment variable value
	val := os.Getenv("WORKING_DIR")
	//Check if the value is set.
	if val != "" {
		fmt.Println("Working Dir is: ", val )
	} else {
		fmt.Println("Working Dir is not set. Setting it now.")
		//You can set env variable using setEnv method.
		os.Setenv("WORKING_DIR", "/root/app_dir")
		fmt.Println("Working Dir is: ", os.Getenv("WORKING_DIR") )
	}

	//You can also iterate over all the environment variables.
	//Here environment variables are returned as string containing varname=value form
	for _, varNVal := range os.Environ() {
		keyVal := strings.SplitN(varNVal, "=", 2)
		fmt.Println("Variable name: " + keyVal[0] + "\tValue: " + keyVal[1] )
	}
}

Golang: Write data to file

Writing data to file is the most common use case while writing a program.  Here we are going to see multiple ways of writing data to file.  

Write using ioutil.WriteFile

You can directly write data to file using ioutil.WriteFile" method. It will take care of creating file if the file does not exists.

package main

import (
	"fmt"
	"io/ioutil"
)

func main() {
	writeStringToFile("data.txt", "Hello File!!!")
}

func writeStringToFile(filePath string, data string) {

	//Need to convert string to byte array.
	//WriteFile will create and write data to file, if file does not exists.
	err := ioutil.WriteFile(filePath, []byte(data), 0644)
	checkNLogError(err)
}

func checkNLogError(err error){
	if err != nil {
		fmt.Println(err)
                panic(err)
	}
}

Write using WriteString

You need to create a file first, using the same file pointer we can write string to file.

package main

import (
	"fmt"
	"os"
)

func main() {
	writeStringToFile("data.txt", "Hello File!!!")
}

func writeStringToFile(filePath string, data string) {

	//Create a file to write data.
	f, err := os.Create(filePath)

	//If there is error to write to file, exit.
	checkNLogError(err)

	//Once file is opened, it should be closed.
	//Defer will take care of it, even if any error.
	defer f.Close()

	//Write data to file.
	_, err2 := f.WriteString(data)

	//Check if there is any error during writing data to file.
	checkNLogError(err2)
}

func checkNLogError(err error){
	if err != nil {
		fmt.Println(err)
		panic(err)
	}
}

Golang: Read data from File ( part II)

In the last post, we have seen how we can read data from the file line by line.  
Sometimes if the file size small then you can read the whole file in one go.  In this example, we will be reading the whole file in one go. 

We will be using 'ReadFile' from 'ioutil' lib to read the file content. 


package main

import (
	"fmt"
	"io/ioutil"
)

func main() {
	readFile("file.path")
}

func readFile(filePath string) {
	//Try to read file contents
	data, err := ioutil.ReadFile(filePath)
	//Check if there is any error reading file contents.
	if err != nil {
		fmt.Println("Unable to read the file content. Error: ", err)
		return
	}
	//Convert the byte data read from file to string and print.
	fmt.Println(string(data))
}

Read also

Golang: Read data from file line by line

Reading data from files is a common use case. Most of the time you need to read data line by line.

There are multiple ways to read data from the file.  

The simplest way is to open a file and use a scanner to read data line by line. The sample code below does the same. 

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	readLineByLine("file_path")
}

func readLineByLine(filePath string) {
	f, err := os.Open(filePath)       //Open file for reading
	if err != nil {
		fmt.Println("Unable to open the file. Error: ", err)
		return
	}
    defer f.Close()     //Close file pointer after reading file.
    
    line := bufio.NewScanner(f)    //Scanner to read line by line
	for line.Scan() {
		fmt.Println(line.Text())
	}
}
Other related posts: 

Golang: How to know IP Address and Hostname

Sometimes you want to know the hostname and IP address of the machine you are running the program on.  We can get the hostname using the OS package.

For IP address we can use the Net package. You can read all IP addresses using InterfaceAddrs, you need to filter out the loopback address to get real IP. 

package main

import (
	"fmt"
	"net"
	"os"
)

func main() {
	//Reading the hostname using OS package.
	hostname, err := os.Hostname()
	if err != nil {
		fmt.Println("Hostname: ", hostname)
	}
	
	//Reading IP Address 
	addrs, err := net.InterfaceAddrs()
	if err == nil {
		for _, a := range addrs {
			if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
				if ipnet.IP.To4() != nil {
					fmt.Println(ipnet.IP.String())
				}
			}
		}
	}
}

Golang: Check if file exists in go lang

It is easy to check if the file with the given path exists or not in Go lang. You can use 'Stat' method from 'os'.  Stat returns a FileInfo describing the named file. So if there is no error then you can safely assume the file exists.    

import (
	"fmt"
	"os"
)

func main() {
	if _, err := os.Stat("file.path"); err == nil {
		fmt.Println("File with given path exists.")
	} else if os.IsExist(err) {
		fmt.Printf("Path info error here. Error: %s", err.Error())
	} else {
		fmt.Println("File does not exist.")
	}
}

Caching is a technique used to store frequently accessed data in a temporary storage layer to improve system performance and reduce latency....