Posts

Showing posts with the label Go Language

Go Language, MYSQL - Simple Rest CRUD API example

Image
In this article, we will show you how to develop a  REST-style web service with Go Language and MYSQL. These are APIs that Spring backend App will export: GET all User's        :   /users GET User by ID     :   /users/{id}  POST User             :   /users PUT User               :   /users DELETE User       :   /users/{id} Technologies used: Go Language MYSQL Database gorilla/mux jinzhu/gorm gorilla/mux:  The gorilla/mux package provides request routing, validation, and other services. jinzhu/gorm:  The gorm is an ORM library for the Go language.GORM provides CRUD operations and can also be used for the initial migration and creation of the database schema. Final Project Structure: Let's start building the application Create database 'userdb': CREATE DATABASE userdb; Initialize the Go project: Initialize the Go project  using the following command go mod init rest-crud-api-go-lang Adding the modules required for the project: go get github.com/gorilla/mux go get github.c

How to add/subtract Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to a date in Go Language

Image
Hello everyone, In this tutorial, we will learn h ow to add/subtract Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to date in Go Language. The time package in Go language provides Add() function which  is used to add/subtract a duration to time t. Since duration can be represented in hours, minutes, seconds, milliseconds, microseconds and nanoseconds, The signature of the Add method is: func (t Time) Add(d Duration) Time Example 1: Go Program to add/subtract minutes. package main import ( "fmt" "time" ) func main() { //Current date and time dt1 := time.Now() fmt.Println( "Current date and time : " , dt1) //Adding 30 minutes dt2 := dt1.Add( 30 * time.Minute) fmt.Println( "After 30 minutes:" , dt2) //Subtract 3 minutes dt3 := dt1.Add(- 3 * time.Minute) fmt.Println( "Before 3 minutes:" , dt3) } Output: Example 2: Go Program to add/subtract seconds. package main import (

How to add/subtract days, months or years to a date in Go Language

Image
Hello everyone, In this tutorial, we will learn h ow to add/subtract days, months or years to a date in Go Language. The time package in Go language provides AddDate() function which is used to add/subtract years, months and days to time t.  The signature of the AddDate method is: func (t Time) AddDate(years int , months int , days int ) Time Example 1: Go Program to add/subtract days. package main import ( "fmt" "time" ) func main() { //Current date and time dt1 := time.Now() fmt.Println( "Current date and time : " , dt1) //Adding 3 days dt2 := dt1.AddDate( 0 , 0 , 3 ) fmt.Println( "After three days: " , dt2) //Subtract 400 days dt3 := dt1.AddDate( 0 , 0 , - 400 ) fmt.Println( "Before 400 days: " , dt3) } Output: Example 2: Go Program to add/subtract months. package main import ( "fmt" "time" ) func main() { //Current date and time dt1 := time.Now()

How to iterate over all the characters of the string in Go Language

In Go Language, a string is a slice of bytes. The bytes of the strings can be defined in the Unicode text using UTF-8 encoding. UTF-8 is based on 8-bit code units. Each character is encoded as 1 to 4 bytes. The first 128 Unicode code points are encoded as 1 byte in UTF-8. For Example, package main import "fmt" func main() { text := "aπ" fmt.Println(len(text)) } Output: 3 Here the length of the String is 3 because, ‘a’ takes one byte as per UTF-8  ‘π’ takes two bytes as per UTF-8 So we cannot use for loop to iterate over all the characters of the string because  it will iterate over bytes and not characters. This means  it will iterate three times and the print value corresponding to a byte at that index. How to iterate over all the characters of the string in Go Language? Iterate over a string by runes  Iterate over a string by for-range loop Iterate over a string by runes Go allows us to easily convert a string to a slice of runes and then iterate over t

How to iterate over words of String in Go Language?

Hello everyone, in this tutorial we will show you how to iterate over all the words of String in the Go Programming language. How to iterate over all the words of the string in Go Language? Using for-range loop package main import ( "fmt" "strings" ) func main() { teststr := "This is a sample sentence." str := strings.Fields(teststr) for _, word := range str { fmt.Println(word) } } Output: This is a sample sentence. More Go language topics, How to make a delay in Go Language How to iterate over all the characters of the string in Go Language Trim a string in Go Language Check if a String is an Integer in Go Language Sort a String Array/Slice in Go Language Program to check if a string contains the substring How to Split a String by Space in Go Language? Trigonometry with Go - Sin, Cos, Tan Functions Examples How to iterate over a map in Go? Go Arrays - Program to Find Largest & Smallest Element Go Arrays - Program t

Trim a string in Go Language - Examples

Image
Hello everyone, In this tutorial, we will show you how to trim all of the leading and trailing spaces in a string. Few methods from the strings package are, Trim():  TrimSpace() TrimLeft() TrimRight() TrimPrefix() TrimSuffix() Example 1:  strings.Trim() function The Trim() function is an inbuilt method of the strings package. This method  will remove the leading and trailing whitespace from a string that is received as a parameter. // Golang program to demonstrate the // example of strings.Trim() Method package main import ( "fmt" "strings" ) func main() { str1 := " My world is too small " fmt.Println( "Before:" , str1) str4 := strings.Trim(str1, " " ) fmt.Println( "After:" , str4) } Output: Example 2:  strings.TrimSpace() function The TrimSpace() function is an inbuilt method of the strings package. This is a better choice if a variety of whitespace runes may be present at the start or end of a