Showing posts with label hello world. Show all posts
Showing posts with label hello world. Show all posts

Sunday, 22 December 2013

Creating a function in golang

Our first piece of code had only one function - main. Let's create a new function in our code and call it from within the main function.
package main

import "fmt"

func helloWorld() {
    fmt.Println("Hello World")
}

func main() {
    helloWorld()
}
This piece of code does the same thing as our first hello world in go from the end user experience. The big difference is we are calling a function. Declaring a new function in go is simple - use the func keyword to begin declaring your function. Then, as in many other languages, include a pair of parentheses. Our hellWorld function takes no arguments at the moment so we leave the parentheses empty. Similarly, helloWorld doesn't return anything, so we can forget about giving our function a return type for the moment. Golang has some C inspired syntax so functions are enclosed in curly braces. The rest of the hellWorld function should look familiar since we are simply calling printing some text to standard out as we did before. Calling a function is equally familiar to anyone with some coding under their belt - as demonstrated in main above where we call our newly defined helloWorld func.

Friday, 20 December 2013

Dissecting Hello World in Go

In the first post, we wrote a simple hello world application in go. Let's examine it a bit and figure out what all the moving parts are. For reference, here's the code again:
package main
 
import "fmt"
 
func main() {
    fmt.Println("Hello World from Golang")
}
We begin our code with the
package main
command. Packages are exactly what they sound like - discreet pieces of code with a name. You can think of the package command as wrapping up your code into a little bundle so it is ready to be delivered and used by another piece of code. Specifying
package main
tells the compiler that this is the entry point of your code. Our import statement allows us to access other packages - it pulls in or imports other bits of code. Here we import the "fmt" package. This package allows us to create formatted input and output. The setup out of the way, we get to the meat and potatoes our
main
function - as the name suggest, main is the function that will get executed. It is the master control point for us to instruct the computer using golang. Our functionality here relies on the fmt.Println function - this prints a line of text to the standard output of the computer (your screen most likely) and appends a new line. And that's it. One super simple go program

Tuesday, 10 December 2013

Hello World from Golang

Every journey into code begins with "Hello World". I don't know why, but that's what we do. It's a safe way of checking that you've got your language installed correctly and can at least run the most basic of basics - that of outputting something (in this case, some text) to the user. Golang, being C-Syntax inspired language, should look fairly familiar to pretty much anyone in the computer sciences. A nice simple Hello World application can be written as follows:
package main

import "fmt"

func main() {
    fmt.Println("Hello World from Golang")
}
Saving the above code as 'hello.go' and then typing the following on the command line:
go run hello.go
should net you a lovely little message:
Hello World from Golang