Learn Go Programming Language - Build RESTful API - Vol ( 1 ) - Neuroon Networks

Breaking

Saturday, May 18, 2019

Learn Go Programming Language - Build RESTful API - Vol ( 1 )

Introduction

 

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. This is a product of google developers. Basically go language is a server side language when consider about web services and web applications.

According to the go documentation, they describe Go Language as below.

Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multi-core and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language. 

 Getting started...

So before everything you need to install go-lang in your computer. Use the given link to go to go-lang website and choose your installation.


Once you finish the installation, you can start coding. You can use any text editor that you like and go-lang supported. I prefer using GoLand by JetBrains. Use the given link if you like to try it.


Hello World with Go... ;)

So once you finish install go-lang and your IDE open a new file, the extention to the file should be *.go. You can use any name for the file. In GoLand you can simply create a file when You navigate to file->new-> <choose go in here>. Then paste the given code to that file and save it.

package main

import "fmt"

func main() {
    fmt.Println("
Hello, World")
}

Save it and let me explain the code for you.

package main -> 

Every Go program starts with a package declaration. Packages are used to organize related go source code files into a single unit and make them reusable.
The package “main” is a special go package that is used with programs that are meant to be executable.

There are two types of programs in Go - Executable Programs and Libraries. Executable Programs are programs that can be run from the command line. Libraries are reusable pieces of code that are used by other programs to perform some task.

import "fmt" ->

The import keyword is used to import reusable pieces of code from other packages to use in our program.

The “fmt” package contains code for dealing with I/O.

func main (){
    ...
} ->  

A function is a unit of code that contains one or more instructions to perform a task. We typically break a program into smaller functions that take some input, do some processing on the input, and produce an output.
A function in go is declared using the func keyword.
The main() function is the entry point of an executable program in Go. It is the first thing that is invoked when you run an executable program.

 fmt.Println("Hello, World") ->

We pass the String “Hello, World” to the Println() function which prints it to the standard output along with a new line. 

Now let's run out code. You can use IDE run support or either you can use your terminal to execute the code for you. I prefer using the terminal. So just paste the given command in the terminal (the current working directory should be the place that you save your go file) 

go run <file name>.go

this will run your code. Please replace the file name with your file name. Or you can build an executable file and run that. To do it,

go build <file name>.go

Then you'll see two files and to run the built file just type ./<file name> 

See, It's  really easy to understand and learn Go. So I highly recomend to read their documentation. You can use this link to visit their official web site and do some self learning, and get some basic knowledge in go-lang before we continue to our API services.

See you in the next article... Happy Coding ;)
 

2 comments:

  1. Awesome Blog, Nicely written and informative. Thanks for sharing… Keep posting.

    Want to build a successful On Demand Gift Delivery App to make a profitable business? Contact The App Ideas!

    ReplyDelete