Wednesday, 6 October 2021

How to Read XML Files From A Directory Golang

Getting Specific Files from a directory by manually can be hard but it can be more hard when you know that there is thousands of and you have to choose only specific extension file. So How do we make it simple to file specific files from directory, So there is the simple answer programming. How? Just Write an script which reads the top level files from a directory and check the extensions of each file, If that file contains the same extension as you wanted then here you go. It's easy to say it in words, so let's start writing some code so you guys can have idea of it..



1. Load Required Flags/Arguments.

We're about to use few flags for this program which is help us to use targeted repository/direcotry full path. We're going to use flags package.

1. First we've loaded the package we're not using go mod so that why we're using direct main package
2. We've created a struct which will going to hold the path of the directory from which we've to extract all the XML files.
3. Created a function loadFlags which have flags package used and that function return the Flag struct with the path value in it.
3. From Line Number 23-30 calling the main function and in the main function we're calling loadFlags function which returns the Flag struct



2. Open Direcotry.

For Opening Folder We're going to use the os package which is inbuild package of Golang

In OpenDir function we're opening the directory by calling the os.Open function if it fails then it will going to return the error other the *os.File which holds information about the file and folders.



3. Get Files and Folder From Directory.

In checkFilesinDir function we're checking for the files and folder inside root path and then after getting any file and folder we're directly appending that value inside a array/slice and then reutring it.



4. Look for Xml files.

Next Step is to check for the xml files array

We've created another function which iterates the loop over the slice of string and check for the .xml extension if any file contains .xml that values will store in the xmlfiles variable, and then returning that back



5. Combine All Functions

Combining all the function into single function and then will going to call this function in the main function.



6. Full Code:

OUTPUT:

Labels: , , , ,

Tuesday, 5 October 2021

Command Line arguments in Golang

So in this article we're about to cover the Command-Line Arguements in Golang, How to get the arguments and parse those arguments into application...

Let’s start writing some programs in Go that will allow us to pass command-line arguments while running a Go program. It's really easy and allows us to pass custom arguments while running a Go program. We will create our custom arguments as well.


Working with Command-Line(CLI) arguments

How to pass the Command-Line arguments? Simply use those in a space-separated way with program name.

./toolname arg1 arg2 arg3


1. The “os” Package of Golang

The os package contains the args which is a array of arguments, that helps use to get the unlimited amount of command-line arguement while running the program/binary. Let’s see our first argument, the program name which you don’t need to pass at all.


The following code will print the program name in the command line.
OUTPUT:



2. How to Get Number of CLI Arguments Used in a Program

We can get the total number of arguments passed through the command-line easily. To acheive that, we uses len() function.

Here is the code that prints the length of total arguments passed to a program.
OUTPUT:



3. Iterating over command-line arguments.

To iterate over the arguments passed via the command line, we will loop over the args variable which holds os.Args array.

See Below to know how to do that
OUTPUT:



4. The “flag” package

To create flags, we'll use a package called flag(inbuilt). We will create a simple login mechanism as an example.

Below is the code for the login system.
OUTPUT:



5. Inserting wrong values in login CLI

Inserting wrong values will give you an error and provide you the correct usage of those flags. For example, if you input a number while using a string flag, an error will be thrown and the correct usage will be printed to the console.
OUTPUT:

Labels: , ,