LEARN GOLANG| Part-4

In case you are landing here directly, it’s suggested that you go and read through this for fundamentals.

In this blog, we shall be looking at following concepts :-

  • Reading a File from Local Directory.

Question:- Let’s demonstrate now process of reading a given input file in Go-Lang ?

  • Here @ line #14, we are specifying the name of the file, from which we are planning to read the data.

Question:- Demonstrate, how do we manage dependencies in GO ?

Answer:- Here is the straight forward way to manage dependencies in Go :-

  • Go Get tool installs one dependency at a time. And it installs the latest version of the package.

Question:- How do we create a module in GO ?

Step #1.) Let’s first create a file called as “go.mod”. You can think of Go.mod as the packages you want.

Step #2.) At line #6, We are adding a requirement by mentioning the name of the package i.e. go-toml package, and we say the version of the package.

Step #3.) We can also see that, the new file is generated, called Go.sum. You can think of Go.sum as the packages that you actually require.

Step #4.) Once you want to upgrade the package, or add a new dependency, edit the Go.mod with a new dependency of the new version. Don’t forget to run your tests after upgrading to a new version.

Question:- Demonstrate, how can we read a Configuration File in GO ?

Answer:- Here is the straight forward way to read the files. Note that, we are making use of go-toml package in order to read the file into an struct automatically :-

Step #1.) We create a configuration object which has the login, which has the user and the password.

Step #2.) We now create the configuration object, into which we shall be reading the information from the configuration file :-

Step #3.) We now install the toml tool first, @ our working directory :-

Step #4.) We now import the necessary package, in order to read the configuration file well :-

Step #5.) Next, we perform following tasks in below snapshot :-

  • At line #20, we now open the file, and as usual, validate the error, which should be not nil.

Question:- Let’s demonstrate now process of generating an output file in Go-Lang ?

  • Here @ line #13, we are specifying the name of the file, that we are planning to generate.

Question:- Let’s demonstrate now process of scrapping some data from Internet in Go-Lang ?

  • Here @ line #12, we are using the http.Get call in order to receive the data from aforementioned URL.

Question:- Let’s now parse the aforesaid JSON-data into some meaningful information ?

Here @ line #47, we created our own custom data-type to convert the each data to the structure type.

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.

References :-

--

--

Software Engineer for Big Data distributed systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store