Advance Programs


How to access a struct from an external package in Go?

You will learn to create your own package and import your custom package. In this example, you will see how to import structure from another or child package. You will also be able to call functions of custom packages from the main package.

Example Explained


Example of Abstraction using Interfaces in Golang

Golang is quite capable of implementing higher level abstractions, but the language designers choose not to implement certain abstractions into the programming language itself. You can use interfaces to create common abstraction that can be used by multiple types. Interfaces define one or more method declarations that must be satisfied to be compatible with the interface.

Example Explained


Golang RESTful API using GORM and Gorilla Mux

REST stands for Representational State Transfer. The name Representational state transfer (REST) was coined by Roy Fielding from the University of California. It is a very streamlined and lightweight web service compared to SOAP or WSDL. Performance, scalability, simplicity, portability are the core principles behind the REST API.

Example Explained


Implementing interface from different package golang

This example aims to demonstrate the implementation of interfaces in Go and import your custom package. You will be able to define and declare an interface for an application in custom packages and implement that interface in your applications.

Example Explained


Example of Golang CRUD using MySQL from scratch

In this tutorial, we are going to see an example program to learn how to do database CRUD operations using Golang and MySQL. CRUD is an acronym for Create, Read, Update, and Delete. CRUD operations are basic data manipulation for database.
In this example, we are going to create an interface as database front end to handle these operations. We have Employee table containing Employee information like id, name and city. With this table, we have to perform CRUD using MySQL.

Example Explained


Golang import function from another folder?

This example aims to demonstrate the various calls of a function in detail. You will learn to create and call a custom package function in main package. You will also be able to call functions of custom packages from the another package using alias.

Example Explained


Code formatting and naming convention tools in Golang

The formatting of code shows the way code is formatted in a file. It points out to the way, how code is designed and how carriage returns used in writing. Go does not require special rules around the code formatting, but have a standard that is generally used and accepted in the community.

Example Explained


Go language Best practices to follow in 2023

Example Explained


Golang program to demonstrates how to encode map data into a JSON string.

The below example is to converts a map type into a JSON string. First we need to add the package encoding/json to the list of imports. Then Marshal function of the json package is used to encode Go values into JSON values.

Example Explained


Dynamic XML parser without Struct in Go

xmlquery is an XPath query package for XML document, lets you extract data or evaluate from XML documents by an XPath expression.

Example Explained


This sample program demonstrates how to create multiple goroutines and how the goroutine scheduler behaves with three logical processors.

The Go standard library has a function called GOMAXPROCS in the runtime package that allows us to specify the number of logical processors to be used by the scheduler.

Example Explained


How to Improve the Performance of Your Golang Application?

The performance of a Go application can depend on several factors, including the complexity of the code, the amount of resources used, and the underlying hardware on which it runs. However, Go is known for its fast execution speed and efficient use of system resources, which makes it a popular choice for building high-performance applications.

Example Explained


Example of Interface with Type Embedding and Method Overriding in GO language

An instance of catalog struct is created by providing values of the two objects that implemented the Information interface. The objects of Information type, are created with the Mobile type and the Shirts type. The value of the Details field contains the value of different types, the connecting factor of all objects is the Information interface.

Example Explained


Golang HTML parser

Package goquery implements features similar to jQuery, including the chainable syntax, to manipulate and query an HTML document.

It brings a syntax and a set of features similar to jQuery to the Go language. It is based on Go's net/html package and the CSS Selector library cascadia. Syntax-wise, it is as close as possible to jQuery, with the same method names when possible, and that warm and fuzzy chainable interface.

Example Explained


Unmarshalling dynamic JSON in Golang

Gabs is a small utility for dealing with dynamic or unknown JSON structures in Go. It does not require you to know the structure of the payload (eg. create structs), and allows accessing fields by providing the path to them. It's pretty much just a helpful wrapper for navigating hierarchies of map[string]interface{} objects provided by the encoding/json package. It does nothing spectacular apart from being fabulous.

Example Explained


How to use WaitGroup to delay execution of the main function until after all goroutines are complete.

If you add a WaitGroup struct to your code, it can delay execution of the main function until after all goroutines are complete. In simple terms, it lets you set a number of required iterations to get a completed response from the goroutines before allowing the application to continue. Done decrements the WaitGroup counter. Wait blocks until the WaitGroup counter is zero.

Example Explained


Golang read file line by line to string

The bufio package Scanner is a suited for reading the text by lines or words from a file. The following source code snippet shows reading text line-by-line from the plain text file below.

Example Explained


Golang Concurrency Best Practices

Concurrency in Go is the ability of a program to perform multiple tasks simultaneously. Go provides built-in support for concurrency through goroutines, which are lightweight threads managed by the Go runtime. Goroutines allow you to perform tasks concurrently without having to manage threads directly. Concurrency in Go is designed to be efficient and scalable, making it well-suited for building high-performance and distributed systems. With its support for concurrency, Go enables developers to write programs that can take full advantage of modern hardware and utilize system resources more efficiently.

Example Explained


This sample program demonstrates how to decode a JSON string.

Go language comes with more flexibility to work with the JSON document. In below example you can decode or unmarshal the JSON document into a map variable. The function Unmarshal of the json package is used to decode JSON values into Go values.

Example Explained