<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vineet Verma on Medium]]></title>
        <description><![CDATA[Stories by Vineet Verma on Medium]]></description>
        <link>https://medium.com/@vkv000?source=rss-589aca97bebf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*ljMi8qaQuTkKP-7oUaqYiw.jpeg</url>
            <title>Stories by Vineet Verma on Medium</title>
            <link>https://medium.com/@vkv000?source=rss-589aca97bebf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 21:44:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vkv000/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[My first 6 hours of development with Go]]></title>
            <link>https://medium.com/@vkv000/my-first-6-hours-of-development-with-go-42a709a766aa?source=rss-589aca97bebf------2</link>
            <guid isPermaLink="false">https://medium.com/p/42a709a766aa</guid>
            <category><![CDATA[software]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Vineet Verma]]></dc:creator>
            <pubDate>Sat, 15 Sep 2018 19:27:22 GMT</pubDate>
            <atom:updated>2018-09-15T19:27:22.790Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BF_ynKXvXqq--vYVDgrRmQ.png" /><figcaption>Image from <a href="https://github.com/ashleymcnamara/gophers">https://github.com/ashleymcnamara/gophers</a></figcaption></figure><blockquote>I found Go would be my next programming language to be learned because it is straightforward for Scripting, DevOps and platform independent binaries.</blockquote><p>I’m using it very first time but it was a very good experience. First <strong>2 hours</strong> were very tough as I couldn’t even understand how to start? I could only configure <strong>Go</strong> in my machine in those hours. I was hitting randomly on web pages and searching for articles to get it done. By profession, I am a web developer having more than <strong>6</strong> years of experience which helped me a lot while building with <strong>Go</strong>.</p><blockquote><em>After all only syntax may change but concepts remain same.</em></blockquote><p>One day I was comparing <strong>Python vs Go</strong> because I love to keep in touch with latest trend, I found <strong>Go</strong> is very interesting. Because it handles concurrency very easily, share all executables with any platform and most important is, it requires least code to write.</p><h3><strong>Background</strong></h3><p>I thought to make a program in <strong>Go</strong> instead of reading only theories. I read some online ideas and simple programs which can help me to understand. From there I got an idea to build own script to extract all images of given URL and save them in a folder.</p><blockquote>Note: This program is just for knowledge to understand go in much better and easier way. I didn’t make this program to scrap data from websites and even I didn’t try it though. But yeah, I’ve tried with my own website.</blockquote><p>After getting hint what to do, I started searching over internet. After installing Go binary, I read about workspaces and what would be the folder structure. As a programmer, I noticed <a href="https://golang.org/doc/effective_go.html">Effective Go</a> covers most of the things. Though this page has theoretical knowledge but yeah, it has many more for a developer. I am trying to explain what I was thinking at the time of development which pointed me to right direction, through this article.</p><p>First I created a new file under <strong>src</strong> folder named it to <strong>go_down.go. </strong>Go has same main() as C.</p><pre>func main() {<br>    response, err := http.Get(os.Args[1])<br>    if err != nil {<br>        fmt.Printf(&quot;%s&quot;, err)<br>        os.Exit(1)<br>    } else {<br>        defer response.Body.Close()<br>        contents, err := ioutil.ReadAll(response.Body)<br>        if err != nil {<br>            fmt.Printf(&quot;%s&quot;, err)<br>            os.Exit(1)<br>        }<br>        fmt.Println(string(content))<br>    } <br>}</pre><p>We have to pass a website <strong>URL</strong> in terminal or while executing this file, to fetch the content and it is handled by os package. All runtime variables are getting extracted in os.Args array. Response of this function is string which has shown in terminal.</p><p>After adding main function I was thinking how to iterate over each line and extract the image links from them. Interesting, right?</p><pre>    scanner := bufio.NewScanner(strings.NewReader(string(contents)))<br>    if isFolderExists(directoryName) == false {<br>        os.Mkdir(directoryName, 0775)<br>    }<br>    for scanner.Scan() {<br>        r := regexp.MustCompile(`(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png|jpeg|JPG|GIF|PNG|JPEG)`)<br>        match := r.FindStringSubmatch(scanner.Text())<br>        if len(match) &gt; 0 {<br>            stringSlice := strings.Split(match[0], &quot;/&quot;)<br>            name := stringSlice[len(stringSlice)-1]<br>            download(match[0], name)<br>        }<br>    }</pre><p>This piece of code will iterate line by line and extract image links by using <a href="https://stackoverflow.com/questions/169625/regex-to-check-if-valid-url-that-ends-in-jpg-png-or-gif#answer-44216396">regex</a>. To use regex, I needed to include regexp package. After extracting links, I wanted to get exact file name of each link so I thought, why not to break string in an array by using a delimiter(thinking as a web developer). I guess every image <strong>URL</strong> is in this format http://www.demo.com/some_image.png (correct me if I am wrong) so here my delimiter was / and <strong>strings.Split</strong> did the trick. In this array at last index, I will be having my exact file name with extension and to get that, I used stringSlice[len(stringSlice)-1]</p><p>All images should be downloaded in a single folder or given path. If the folder doesn’t exists, I’ve to create a new folder.</p><pre>//Check there is already a folder on given path<br>func isFolderExists(path string) (bool) {<br>    _, err := os.Stat(path)<br>    if os.IsNotExist(err) {<br>        return false<br>    }<br>    return true<br>}</pre><p>Above script will check whether mentioned folder exists or not. If not, then we can create a folder by using os.Mkdir(directoryName, 0755) Second argument is showing folder’s permission. Now finally I needed to put logic to save image</p><pre>func download(url string, name string) {<br>    response, e := http.Get(url)<br>    <br>    if e != nil {<br>        log.Fatal(e)<br>    }</pre><pre>    defer response.Body.Close()</pre><pre>    file, err := os.Create(getDirectoryPath(name))<br>    if err != nil {<br>        log.Fatal(err)<br>    }<br>    _, err = io.Copy(file, response.Body)<br>    if err != nil {<br>        log.Fatal(err)<br>    }<br>    file.Close()<br>}</pre><h3>How to build and run the code?</h3><p>At the time of development, I didn’t use go build every time to see the changes and it is not a good practice though. I used go run go_down.go https://myteamteck.com/team for execution. In last when I am done with the code, I used go build go_down.go to convert code into executable binary. Now you can see there is an executable file named go_down. On the same directory, use go_down https://myteamteck.com/team to extract all of the images of this URL. In my case, I used images folder in same directory. If everything goes well, you can see images folder is containing all images.</p><h3>How was my first experience with Go?</h3><p>It was really amazing. Though <strong>Go</strong> is a new programming language but it has many built in packages which helped me a lot. As it is an open source platform, so got many hints over <strong>stackoverflow</strong> and <strong>git</strong> but in last, I’m happy that I’d developed my first program with <strong>Go</strong> in first <strong>6 hours</strong>. Those hours were productive and I’ve a solution with me. You can download complete code with my <a href="https://gist.github.com/vineet-287/b4b67167cce51e143115051af6da2cef">gist</a> and share your observations down in comments.</p><p>If you like this article then please express your interest by clicking on those green left hands.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42a709a766aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>