Categories
Uncategorized

The “Go” Programming Language: A First Look [Part-1]

It’s been a while I am looking at Go. The language is nice, feels like a mixture of Python and C with huge performance gains and some innovative language features. In this post, I would try to quickly focus on the basics of “Go”. I hope I shall write more on the language in the coming days.

Installation

Installation is really platform specific. I would happily forward you to http://golang.org/doc/install for installation instructions on your OS.

If you happen to have the same OS as mine, that is OSX, I highly recommend installing Go using Homebrew. It’s just a matter of one line –

Once it’s installed, you can display useful information with this command –

If you are on Windows, Linux or any other OS, please follow a suitable instruction set from the documentation.

Running Go Programs

How does a first program look on Go? Let’s see –

A Go file must define a function named main if you want to run it. We can define a function in go using “func” keyword.

Running it –

You can directly run a Go file by using the “go run” command. This comes in handy for debugging, if you want to build a compiled binary, use “go build”.

Variables, Constants and Printing

First, how do we define variables in Go? It’s simple –

In Go, we can import packages using the “import” keyword. The package “fmt” has a nice function named “Println” which is more or less Go equivalent to System.out.println() in Java. Let’s see a code sample –

If you are initializing a variable, you can optionally skip the variable data type. Go can infer that from the initialization.

There is a shorthand format for declaring and initializing a value quickly –

It will create a var type string and assign the value “masnun” at the same time.

To create a constant, we just replace “var” with “const”. Example:

Looping

In Go, we use “for” for all types of looping. Check out –

While Loop:

Generic For Loop:

We can do common “foreach” loops combining “for” with “range”. We shall see them in the next section.

Arrays, Slices and Maps

Arrays:

If you are any programmer at all, I probably don’t need to tell you about arrays. Defining arrays in Go is easy.

The syntax is –

We can reference the indexes just like in any other language. We can even declare and initialize an array on the same line. Take a look at the example:

Slices:

Slices are like arrays but not fixed size. However, the type must remain the same. I mean you can’t add a string type to an integer slice.

We use the make() function to create a slice. In fact, in Go, make() can create a lot of stuff for me. We shall soon find out. For now, let’s see how we can use a Slice.

Maps:

Maps are what we call “Hashes”, “Dictionaries” or “Associative Arrays” in misc other languages. Maps are in reality key value pairs. We use make() to create maps of certain types. Example follows –

Iteration with Range

“range” allows us to iterate over a number of data structures. range gives us the key and value in each iteration. It works quite like the “enumerate” function in some languages (eg. Python). Let’s see how we can iterate over common data structures:

That’s all for Part – 1. In my next blog post, I shall try to cover “If/Else” with “Structs, Functions & Methods”.

Categories
PHP

Laravel 4: Sending Emails

Laravel 4 uses SwiftMailer library behind the stage. They have wrapped the SwiftMailer functionality in elegant and easy to use APIs for delivering emails.

The Mail::send() API is straight forward, it takes these as argument –

# The email template to render. You can pass an array for sending both text and html emails.
# The data array which contains the values for the email template.
# A closure to configure the message. The closure is passed an instance of SwiftMailer and you can use the same set of configuration you can use with SwiftMailer.

Choosing a Mail Transport

Laravel 4 allows easy configuration of the mail transport in app/config/mail.php file. The framework has drivers for smtp, PHP’s mail() function and sendmail. If you have a local smtp server running, choose the smtp driver. Set the host to localhost and port to 25.

Pretend Sending Mail

Laravel4 has a nice feature for testing emails on local machines where you probably don’t have a mail server installed and/or configured. You can switch “pretend” in the above configuration to true or use Mail::pretend(TRUE) before making a call to Mail::send(). This will not try to deliver the mail but make a log in the application log file. You can find the logs files in – “app/storage/logs” directory.

Categories
Javascript

Log to Firebug Console from Firefox Extensions

This is a quick and short post, mainly for self documentation. If you are working on a Firefox extension and want to do “console.log()” to Firebug, you need to use this instead –

You can’t directly use console.log() because your code doesn’t execute from within a “window” instance. If you get “Firebug” is undefined or some similar error messages, make sure that Firebug is installed and working properly.

Firebug needs to be running when you try to log the strings.