Writing First Program in GO!
Dat One Dev

Dat One Dev @dat_one_dev

About: Just a programmer , creator , student and your friend

Joined:
Feb 21, 2025

Writing First Program in GO!

Publish Date: May 19
0 0

Introduction

Previously, we covered what GO is and why we should use it.

I hope that after reading my previous post, many people have become interested in the GO programming language.

As a programmer myself, when I first heard about GO, it immediately took my interest.

In the past, I had also found Rust interesting, but it was too hard to start.

I had also tried to learn the basics of C, but I did not feel the spark, I feel in programming languages that attract me. (For example, GD Script, Lua)

When I tried GO, I felt that spark, and then I knew this was the language I was in search of, and thus I started my journey of GO.

Welcome to the Journey of GO! Let's put our first step down.

Creating our first program

What are we making?

Today, I am planning to create a birthday invitation program.

(And creating a birthday invitation program is nothing to relate to my birthday coming on 21st May, which unfortunately I have to celebrate in the hospital 😉😢)

Starting with GO. (package, import, and main function)

To do this, first, we have to create a new GO file.

  • Step 1: Create BirthdayInvitation.go to a new empty folder named GO

However, you can name the program and folder as you want.

  • Step 2: Get done with the GO program basic structure.

Now, first let's just try printing "Hello, World!"

package main

import (
)

func main() {
}
Enter fullscreen mode Exit fullscreen mode

This is how the most GO codes at the start look. Let's understand what each line means:

  • package main: package is a GO keyword that tells which code bundle this file belongs to. There can only be 1 package per fold. This code belongs to the main package.

  • import (): This is the keyword that tells the GO compiler which other packages you would like to reference or import in your file.

  • func main(){}: This is the starting point of any GO application. Code written here executes as the program starts.

Importing the package.

As the structure of our GO program is done, now we need to add some basic functionality. Let's try printing "Happy Birthday to x." Here we can suppose x is the user's name.

To do this, first, we need to import a new package.

  • Step 3: Import the fmt package.

fmt is a package that provides basic formatting and printing functions.

To import this, we just need to do.

package main

import "fmt"

func main() {
}
Enter fullscreen mode Exit fullscreen mode

We don't need to download the package separately as it's already available in the Standard Library.

Printing.

Now let's try printing something. In Go, we use Printf() to print somethin., Although we can also use Println() but it doesn't help us to inject variables between strings easily.

package main

import "fmt"

func main() {
    fmt.Printf("Happy Birthday to X")
}
Enter fullscreen mode Exit fullscreen mode

First, we reference the package and use the function.

Here is how we print in some other languages:

  • Python: print()
  • JavaScript: console. log
  • Miniscript: print ""
  • C: printf()

Declaring a variable

  • Step 4 - Declaring input variable Now we will need to ask for user input, but to do so, first we need to declare a variable which store user input.

As we know, GO is Statically typed, thus we need to declare the data type of the variable while declaring the variable. But we can take the help of type interfacing in GO and skip this.

name := "Selfish_Dev"
Enter fullscreen mode Exit fullscreen mode

But now we've just found that we don't know user names, so declaring the value of the variable is a dumb idea.

Thus, type interfacing wouldn't work. A better way to declare variables when you don't know their type is like this:

var name string
Enter fullscreen mode Exit fullscreen mode

This declares an empty variable "name" with data type string and value null.

Although this would work too.

name := ""
Enter fullscreen mode Exit fullscreen mode

I have a specific post on this topic, Variables in GO, so I highly recommend checking it out. It's a
short read.

Using a variable to store user input

Now, as we have a variable, we can store user input. Before that, we need to prompt them to enter their name with the help of Printf statements, which we have already covered, so try it yourself.

  • Step: Using the Printf statement to direct the user to what to do.

Now we need to use the input for this. When we use another function, we use the fmt package.

Scanln() - Ask user for input and stores input in provided variable.

package main

import "fmt"

func main() {
    name := ""
    fmt.Printf("Enter Your First Name Please:")
    fmt.Scanln(&name)
}
Enter fullscreen mode Exit fullscreen mode

Now the program asks the user to input their name and stores it in the name variable.

Printing Happy Birthday Statement

Now, at last, we need to print the happy birthday statement.

package main

import "fmt"

func main() {
    name := ""
    fmt.Printf("Enter Your First Name Please:")
    fmt.Scanln(&name)
    fmt.Printf("Happy Birthday %s", name)
}
Enter fullscreen mode Exit fullscreen mode

%s - Format Specifier for String.
name - Variable

Outro

Other Post: What is GO
YouTube Channel: Selfish_Dev

Drop your thoughts down in the comments

Till then, stay Awesome and stay Selfish.

Comments 0 total

    Add comment