The Beginning

The Beginning

Hello, everyone

To help myself and you, the readers, be a better developer, and to encourage everyone's learning through conversations in comments, I have decided to start writing small articles on here about my learnings as I build stuff in my time.

Today, I'm gonna write about my experience with Golang so far coming from an Express.js background.

My Golang Setup

I started learning Go for building APIs. My first mini-project, just to get familiarized with the language was a basic authentication backend. I used MongoDB as the database, JWT for authentication, and the Go Fiber module to make creating routes easier. I challenged myself to write clean code, segregate code into appropriate folders/directories.

Coming from Node.js

Having built a few projects in Node.js, here are a few differences and similarities that come to my head

Anologous Elements

Development in Go and Node.js is very similar, and the following can be considered analogous to each other

  • go.mod is analogous to package.json

It consists of all the external modules we are using in our project. Go also has a go.sum file which consists of a checksum for all of the modules

  • go mod init <module-name> is analogous to npm init <module-name>
  • go get . is analogous to npm install
  • go run main.go is analogous to node index.js

No more (manual) importing and exporting

We use camelCase (or CamelCase) to name everything in Go (there might be a few exceptions), and the capitalization of the first letter decides if it can be used in other files

var schoolName string // This cannot be used elsewhere
var SchoolLocation string // This can be used in other files        

Packages

In Go, every folder in the codebase is called a package, and has the name of the folder. The root folder, that is where your entry point will be (main.go, analogous to index.js), is the 'main' package.

Whenever we create a file in a folder, we need to declare the package name at the top of the file. For example, the beginning of a file /controller/auth.go would be

package controller        

When using a code block from a package in another package, we simply type <package_name>.<element>. For example, if the package controller has function

func Add(int a, int b) int {
   return a + b        

We can use it in another package as

sum := controller.Add(5, 4)        

The IDE will take care of importing the package at the top of your file after you save your work. I was using VSCode with the official Go extension from go.dev.

Structs

In Go, we need to explicitly state what datatypes we're gonna use, through structs, instead of using objects like in javascript.

For example, let's say the request's body for the API call is

{
    "email": "[email protected]",
    "password": "SomethingSecure"
}        

In node, you can directly access the elements of the req like req.body.email

But in Go, you need to create a Struct denoting the structure of the request that is to be handled, and then you need to decode the json into an instance of the struct.

type Body struct {
    Email    string `json:"email"`
    Password string `json:"password"`
}
// notice how we needed to explicitly state what the name for the field in json would be

var bodyInstance Body // creating an instance

// the following code is specific to Go Fiber
err := c.BodyParser(&bodyInstance) // yes, we pass the address

// the following is how we perform error handling in Go instead of try catch
if err != nil {
    panic(err)
}        

Conclusion

These are the major things that come to my head as of writing this. There are of course many other differences one will notice while building something in Go.

I find writing backend code in Golang to be much more easier, and faster than in Javascript. Mainly because of the exporting and importing. Surely the syntax is wacky at first, but I now prefer Go over Node.js for building the backend for my future projects.

Feel free to ask anything in the comments, or even correct me wherever I'm going wrong in my understanding.

Thank you for reading.

Aamir Alam

Golang || NextJS || PHP || Laravel || Codeigniter

6 个月

Really informative ??

要查看或添加评论,请登录

Yash Jaiswal的更多文章

  • HTTP Methods, CRUD Operations, and cURL

    HTTP Methods, CRUD Operations, and cURL

    According to the MDN Docs, there are 9 request methods defined by HTTP (HyperText Transfer Protocol). When we're…

  • Building a Basic Server with Golang

    Building a Basic Server with Golang

    Today, let's build a basic server with Go, using only the built-in modules. First let's understand what a server is.

社区洞察

其他会员也浏览了