Writing your first Go programme
Tags: beginners, tutorial, go
Published: 2019-05-26 14:23:02

---
title: Writing your first Go programme
published: true
tags: beginners, tutorial, help, go
canonical_url: https://www.codetips.co.uk/languages/go/writing-your-first-go-programme/
---

> By the end of this article, you will have written a Go programme that outputs a sentence based on a number of variables.

 ![Writing your first Go programme](https://images.unsplash.com/photo-1526374965328-7f61d4dc18c5?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

If you've made it to this article you should have read the [Go Introduction](https://www.codetips.co.uk/languages/go/go-introduction/), and be familiar with [variables](https://www.codetips.co.uk/beginner/what-is-a-variable/) and [data-types](https://www.codetips.co.uk/beginner/what-is-a-data-type/).

The [syntax](https://www.codetips.co.uk/beginner/what-is-syntax/) for declaring a variable in Go defines a few ways of declaring a variable, which we're going to break down into two groups.

### Explicitly typed

If the programmer specifies a [type](https://codetips.co.uk/beginner/what-is-a-data-type/) when declaring a variable, it is said that the variable is explicitly typed.

In the following example:

- `x` is declared as a `string`, and the value of `x` is `"Hello World"`.
- `y` is declared as a `bool`, but is not assigned a value\*.
- `a` and `b` are both declared as `int`, and given their respective assignments ( `a=1`, `b=2` ).

<!--kg-card-begin: code-->
```
var x string = "Hello World"

var y bool

var a, b int = 1, 2
```
<!--kg-card-end: code-->

### Inferred Type

If the programmer does not specify a [type](https://codetips.co.uk/beginner/what-is-a-data-type/) when declaring a variable, it is said that the type of the variable is inferred from the assignment value.

In the following example:

- `x` is assigned to a value of `"Hello World"` and has an inferred type of `string`.
- `y` is assigned to a value of `true` and has an inferred type of `bool`.

<!--kg-card-begin: code-->
```
var x = "Hello World"

y := true
```
<!--kg-card-end: code-->

If you need to be absolutely sure that a variable is of a specific [data-type](https://www.codetips.co.uk/beginner/what-is-a-data-type/) (e.g. a `float` instead of an `int`), use the explicitly typed method, otherwise, use common sense for which is better in each circumstance.

\* When a variable is declared without an initial value, they are given the zero value. This is `0` for numeric types, `false` for booleans and `""` (an empty string) for strings.

<!--kg-card-begin: hr-->
* * *
<!--kg-card-end: hr-->
### Your first Go programme

It's finally time to write your first Go programme. When [installing](https://golang.org/doc/install) Go, you would have set-up your Go directory ( e.g. `/usr/local/go` in Linux environments ).

Within this directory, create a new directory under `/src/firstApplication` (e.g. the full Linux path would be `/usr/local/go/src/firstApplication` ) and open it in your code editor of choice.

We're going to leave out most of the specifics of Go, so you're not overwhelmed with too much information, but everything will be explained in future articles.

In your new `firstApplication` directory, create a new file called `main.go`, add the below code into it, and save the file.

<!--kg-card-begin: code-->
```
package main

import "log"

func main() {

	// Start of our code

	log.Println("Welcome to my first Go programme.")

	// End of our code

}

```
<!--kg-card-end: code-->

Now, open a terminal, navigate to your `firstApplication` folder, and run the following command:

<!--kg-card-begin: code-->
```
go run main.go
```
<!--kg-card-end: code-->

Your output should look very similar to the below.

<!--kg-card-begin: image-->

![Writing your first Go programme](https://www.codetips.co.uk/content/images/2019/05/image-6.png)

<!--kg-card-end: image-->

As you can see, the `log.Println` function outputs text to the terminal. Unfortunately, as a first programme goes, this is pretty boring. Let's make this more personal with [Variables](https://www.codetips.co.uk/beginner/what-is-a-variable/)!

Replace the contents of `main.go` with the following code:

<!--kg-card-begin: code-->
```
package main

import "log"

func main() {

	// Start of our code

	var name string = "Simon"
	var age int = 29
	var profession string = "Developer"

	log.Println("Welcome to my first Go programme.")
	log.Printf("My name is %s", name)
	log.Printf("I am %d years old", age)
	log.Printf("I work as a %s", profession)

	// End of our code

}
```
<!--kg-card-end: code-->

This time we're outputting four different sentences. One from the previous example and the other three are using a new function called `Printf`. This function allows the programmer to pass in one or more variables; by specifying the variables [data-type](https://www.codetips.co.uk/beginner/what-is-a-data-type/) ( `%s` for a `string` and `%d` for an `integer` ).

Now see if you can complete the following challenges:

1. Change the variables so the programme prints out your name, age and profession.
2. Change the programme to output everything in one sentence, instead of four different sentences.
3. Add a new variable, `favouriteAnimal` , and incorporate that into your outputted sentence. 

Tweet us at [@RealCodeTips](https://twitter.com/RealCodeTips) and show us your solutions. If you get stuck along the way, reach out and we'll do our best to help.