Annie – EntryPoint [#1]

Entrypoint

The main function in the main.go file is the entry point of any go program.

The first few lines in this file are imports for packages needed in this file. Then there’s a big fat block of variable declarations, most of which will contain the values of command line arguments.

var ( 

    version bool 
    debug bool 

) 

Is a much cleaner way of declaring variables especially when they are a lot compared to

var version bool 
var debug bool 
var blahblah whateverTypeUnderTheSun 

The init function

After the variable declarations, there is an init function. The init function can be seen as a setup function, used to initialize preferred program state. It is usually called before any other function in a package and is called only once by the go runtime. More information about the init function can be found in the language spec document at https://golang.org/ref/spec#Package_initialization

In the init function in this program, the flags are defined and ‘connected’ to the variables declared above.

The first flag definition is

flag.BoolVar(&version, "v", false, "Show version")

Looking at the documentation for the flag package , the function signature for flag.BoolVar is

func BoolVar(p *bool, name string, value bool, usage string)

The function receives a pointer to a variable, which is the variable we want the value of the flag to be stored, a name for the flag, a default value and a usage description.

The documentation of the flag package can be consulted for explanation on other flag types.


The main function

In the main function, the first line is flag.Parse() which will parse the command line arguments and set the values of the respective flag variables. You will notice that os.Args[1:] is not passed to this function. The flag package retrieves the command line arguments implicitly, so you do not have to bother about that.

Further down, checks are done on some flags and appropriate action carried out.

A file that should contain a list of urls to download is then passed and the values appended the args variable. args contains a slice of non-flag command line arguments returned from calling flag.Args()

Towards the end of the file, it sets default request options and then proceeds to download the file from each url using the download function in the main.go file


The download function

The download function takes the video url as an argument.

It calls extractors.Extract() passing the video url and a struct of values as option, most of the values coming from command line arguments. Extractor gets some data from the url passed that the downloader will need to download the required file, there will be more on this function in the next post.

After extracting data, a new instance of the downloader is created using the downloader.New() function, and the downloader.Download() function is called to start the process of downloading the file.

This is an overview of what happens in the main.go file. Non trivial parts like error handling and things that are seemingly clear have been left out to focus on real details. Should you not understand things that have not been explained in this blog post, drop a comment and I’ll be sure to reply you.

One thought on “Annie – EntryPoint [#1]”

Leave a Reply

Your email address will not be published. Required fields are marked *