<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: menarayanzshrestha</title>
    <description>The latest articles on DEV Community by menarayanzshrestha (@menarayanzshrestha).</description>
    <link>https://dev.to/menarayanzshrestha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F288510%2F5c844f93-d8ab-4300-8def-46852d8bdf54.jpeg</url>
      <title>DEV Community: menarayanzshrestha</title>
      <link>https://dev.to/menarayanzshrestha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/menarayanzshrestha"/>
    <language>en</language>
    <item>
      <title>Send Sms with Twilio on golang</title>
      <dc:creator>menarayanzshrestha</dc:creator>
      <pubDate>Tue, 08 Mar 2022 16:09:43 +0000</pubDate>
      <link>https://dev.to/menarayanzshrestha/send-sms-with-twilio-on-golang-1ied</link>
      <guid>https://dev.to/menarayanzshrestha/send-sms-with-twilio-on-golang-1ied</guid>
      <description>&lt;p&gt;Twilio is an American cloud communications platform. Working with golang is easier with twilio. &lt;/p&gt;

&lt;p&gt;Lets signup for twilio account:  Twilio | Try Twilio Free &lt;/p&gt;

&lt;p&gt;Enter the details and get the account. Please verify your email and your mobile number with the flow of the link. We need three things to work with twilio on golang from Project Info.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AUTH SID&lt;/li&gt;
&lt;li&gt;AUTH TOKEN&lt;/li&gt;
&lt;li&gt;PHONE NUMBER( Sender phone number)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Generate all these variable which are required for twilio api.&lt;/p&gt;

&lt;p&gt;Let code some golang for integration with twilio:&lt;/p&gt;

&lt;p&gt;First run this command to create go.mod&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;go mod init github.com/menarayanzshrestha/workwithtwilio&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;and create a file main.go&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, Golang Dev"))
    })

    PORT := "5000"

    fmt.Println("Serverr running on port:", PORT)

    http.ListenAndServe(":"+PORT, nil)

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run this code and make sure the server is running. If you wake to work with hot reload follow the article on &lt;/p&gt;

&lt;p&gt;Since we have to save the auth variables on code which is not a good practice. So let us integrate to pull the variables from .env file&lt;/p&gt;

&lt;p&gt;Run this command to get the package.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;go get -u github.com/joho/godotenv&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;And  create the folder on root file named utils and under it create a file named “env.go”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package utils

import (
    "log"
    "os"

    "github.com/joho/godotenv"
)

//GetEnvWithKey : get env value of provided key
func GetEnvWithKey(key string) string {
    return os.Getenv(key)
}

//LoadEnv loads environment variables from .env file
func LoadEnv() {
    err := godotenv.Load(".env")
    if err != nil {
        log.Fatalf("Error loading .env file")
        os.Exit(1)
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add this code on the main.go on the top of main function&lt;/p&gt;

&lt;p&gt;utils.LoadEnv()&lt;br&gt;
This will load the .env file &lt;/p&gt;

&lt;p&gt;Lets make .env file in root&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#twilio
SID=AC0e05cb5549fbc7ebbebf577*****
AUTH_TOKEN=aeb3c88f1306799550af2c*****
TWILIO_NO=+170888*****
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: This is just a sample value. You need to get them from twilio dashboard. &lt;/p&gt;

&lt;p&gt;Now create a file “twilio.go” under utils folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package utils

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "net/url"
    "strings"
)

func SendSms(to string, message string) {

    getEnv := GetEnvWithKey

    // Set account keys &amp;amp; information
    accountSid := getEnv("SID")
    authToken := getEnv("AUTH_TOKEN")
    from := getEnv("TWILIO_NO")

    urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json"

    msgData := url.Values{}
    msgData.Set("To", to)
    msgData.Set("From", from)
    msgData.Set("Body", message)
    msgDataReader := *strings.NewReader(msgData.Encode())

    client := &amp;amp;http.Client{}
    req, _ := http.NewRequest("POST", urlStr, &amp;amp;msgDataReader)
    req.SetBasicAuth(accountSid, authToken)
    req.Header.Add("Accept", "application/json")
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

    resp, _ := client.Do(req)
    if resp.StatusCode &amp;gt;= 200 &amp;amp;&amp;amp; resp.StatusCode &amp;lt; 300 {
        var data map[string]interface{}
        decoder := json.NewDecoder(resp.Body)
        err := decoder.Decode(&amp;amp;data)
        if err == nil {
            fmt.Println(data["sid"])
        }
        log.Print("------------Sent sms successfully------------")
    } else {
        fmt.Println(resp.Status)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to run the SendSms function in main.go. So add this inside “/ “ HandleFunc.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;utils.SendSms("+9779800000000", "Test Message from twilio")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note: “9800000000”is just the sample phone number . You need to verify the number first before able to send sms in trial version.&lt;/p&gt;

&lt;p&gt;So lets start the server and hit &lt;code&gt;http://localhost:5000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Hope it works.&lt;/p&gt;

&lt;p&gt;Happy coding. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Hot Reload using Air in golang</title>
      <dc:creator>menarayanzshrestha</dc:creator>
      <pubDate>Tue, 08 Mar 2022 16:05:16 +0000</pubDate>
      <link>https://dev.to/menarayanzshrestha/hot-reload-using-air-in-golang-5go3</link>
      <guid>https://dev.to/menarayanzshrestha/hot-reload-using-air-in-golang-5go3</guid>
      <description>&lt;p&gt;Hot reloading is the important and most frequently used feature. We play and change different messages, logic inside our code. There is an easy way to implement it.&lt;/p&gt;

&lt;p&gt;At first let us create a http server on golang: &lt;/p&gt;

&lt;p&gt;Create a file named “server.go” inside which copy the below code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, Golang Dev"))
    })

    PORT := "5000"

    fmt.Println("Serverr running on port:", PORT)

    http.ListenAndServe(":" + PORT, nil)

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a http-server hosted on port “5000”. To run server&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;go run server.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;You can see the message : “Server running on port: 5000”. To verify it go to the browser and hit the url : &lt;a href="http://localhost:5000/"&gt;http://localhost:5000/&lt;/a&gt; . You can see “Hello, Golang Dev” as response. Its working right ??&lt;/p&gt;

&lt;p&gt;Now if you want to change that message to “Hello World” . You need to restart server which is time consuming. There are many ways to hot reload. Here will talk about the “Air”.&lt;/p&gt;

&lt;p&gt;Air  is a command-line utility that provides live reloading for Go applications. To install it&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;go get -u github.com/cosmtrek/air&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Next create .air.conf on the root directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# .air.conf
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format

# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "." 
tmp_dir = "tmp"

[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./tmp/main ."
# Binary file yields from `cmd`.
bin = "tmp/main"
# Customize binary.
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Watch these directories if you specified.
include_dir = []
# Exclude files.
exclude_file = []
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop to run old binary when build errors occur.
stop_on_error = true
# This log file places in your tmp_dir.
log = "air_errors.log"

[log]
# Show log time
time = false

[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
# Delete tmp directory on exit
clean_on_exit = true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First check air is install properly or not.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;air -v&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;If it works yo are ready to go. Else you need to setup for path for “air”&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo mv ~/go/bin/air /usr/local/bin&lt;/code&gt;&lt;br&gt;
Recheck &lt;/p&gt;

&lt;p&gt;&lt;code&gt;air -v&lt;/code&gt;&lt;br&gt;
Finally, instead of running our Go application with the usual “go run server.go” command, use the “air” command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;air&lt;/code&gt;&lt;br&gt;
Hope it works.&lt;/p&gt;

&lt;p&gt;Happy Coding&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
