Go

Golang: Find the type of the variable

Golang: Find the type of the variable The Go reflection package has methods for inspecting the type of variables. The following snippet will print out the reflection type of a string, integer and float. Document https://golang.org/pkg/reflect/#Type package main import ( "fmt" "reflect" ) func main() { b := true s := "" n := 1 f := 1.0 a := []string{"foo", "bar", "baz"} fmt.Println(reflect.TypeOf(b)) fmt.Println(reflect.TypeOf(s)) fmt.Println(reflect.TypeOf(n)) fmt.Println(reflect.TypeOf(f)) fmt.Println(reflect.TypeOf(a)) } How to print variable type The Printf is capable of print exactly variable type using %T formatting

Read more →

Golang: Generate fixed size random string

Golang: Generate fixed size random string There are many ways you can generate random string, In this article I will explore fastest method to generate fixed size random string Generate Random String using rand.Intn() This is most simplest way to generate random string but not slowest (ASCII table http://www.asciitable.com/) //RandomString - Generate a random string of A-Z chars with len = l func RandomString(len int) string { bytes := make([]byte, len) for i := 0; i < len; i++ { bytes[i] = byte(65 + rand.

Read more →

Golang: parse and format unix timestamp

1. If you have unix timestamp in String The time.Parse function does not do Unix timestamps. Instead you can use strconv.ParseIntto parse the string to int64 and create the timestamp with time.Unix: package main import ( "fmt" "time" "strconv" ) func main() { i, err := strconv.ParseInt("1518328047", 10, 64) if err != nil { panic(err) } tm := time.Unix(i, 0) fmt.Println(tm) } 2. If you have unix timestamp in int The time.

Read more →

Golang: Serialize struct using gob: Part 2

[Golang: Serialize struct using gob — Part 2 In this article we will explore following functions of gob func (dec \*Decoder) Decode(e interface{}) error func (enc \*Encoder) Encode(e interface{}) error Encode and Decode functions are helpful when you want to write network application. Example 1: Simple encoding and decoding student structure package main import ( "fmt" "encoding/gob" "bytes" ) type Student struct { Name string Age int32 } func main() { fmt.

Read more →

Golang: Serialize struct using gob: Part 1

Golang: Serialize struct using gob: Part 1 Serialize of the struct will help you to transfer data over network or it will help you to write data on disk. In a distributed system you generate data then serialize, compress and send. On other end you receive data then decompress, deserialize and process. The entire process must be fast and efficient. Go lang has it’s own serialize format called gob. Using gob you can encode and decode structure.

Read more →

Golang gracefully stop application

Golang: Gracefully stop application To shutdown go application gracefully, you can use open source libraries or write your own code. Following are popular libraries to stop go application gracefully https://github.com/tylerb/graceful https://github.com/braintree/manners In this article, I will explain how to write your own code to stop go app gracefully Step 1: make channel which can listen for signals from OS. Refer [os.Signal] package for more detail. os.Signal package is used to access incoming signals from OS.

Read more →

Golang: Send GCM - Google Cloud Message to Android Device

Assumption Go lang is installed in your computer Android sample application with GCM support Step1: Generate server API key for GCM, This article will help you to generate server API key: [https://support.clevertap.com/docs/android/how-to-find-your-gcm-sender-id-and-gcm-api-server-key.html] Step2: Install Go library for GCM $ go get github.com/google/go-gcm Step3: Write the following function in your main.go, replace server API key in the following function. You can provide multiple client tokens in “regIDs” in case, you would like to broadcast message to multiple devices.

Read more →

Golang: Send Push Notification to iOS device

Assumptions Go language is installed in your computer iOS sample application with APNs support for testing Step1: Generate certificate PEM file. This article will help you to generate PEM [https://www.raywenderlich.com/123862/push-notifications-tutorial] Step2: Install Go library for APNs go get github.com/anachronistic/apns Step3: Write the following function in your main.go. Place certificate PEM file in config folder. You can change push URL as per your requirement. func SendPushToClient(pushText string,pushToken string) { fmt.

Read more →

Go Language for Java Developers Part 4

Following keywords are reserved and may not be used as identifiers. Java Keywords Go Lang Keywords There are few obvious keywords like break, case, if, for, etc but few keywords are new in Go Language. func: To declare the function interface: To declare the interface (It’s different than Java’s interface) defer: something like finalise method in Java go: To create a thread chan: To do synchronised between threads var: To declare a variable range: It’s like an iterator in Java

Read more →

Go Language for Java Developers Part 4

Variable: A variable is a storage location for holding a value. The set of permissible values is determined by the variable’s type. Java language has primitive type and objects, both have different syntax to declare a variable either Primitive type or Object type. Animal a = new Animal() Student s = new Student() Java is object oriented language so that we can have access modifier for variable declaration private int a public String b protected float c private Animal a In the Java we can declare a variable at many places like Local variable, Parameters, Class level, Instance variable.

Read more →

Go Language for Java Developers Part 1

Go language is normally known as golang. It’s general purpose programming language developed at Google in 2007 by three Google employees namely Robert Griesemer, Rob Pike, and Ken Thompson. In November 2008 Google had announced Go Language to public and made it [open source]. Go language compiler is available for the Linux, Mac OS X, FreeBSD, NetBSD, OpenBSD, Plan 9, and Microsoft Windows operating systems and the i386, amd64, ARM and IBM POWER processor architectures.

Read more →

Go Language for Java Developers Part 2

Normally first program you write in any programming language is “Hello World”. Hello World is simple program which print “Hello Word” text on console / screen. Java: Hello World As Java Developer you can easily understand following code. No need to explain. correct ? package com.kpbird.gotutorial; public class Main { public static void main(String[] args) { System.out.println("Hello World"); } } To compile & execute above code, you need to write following two commands in the Terminal.

Read more →

Go Language for Java Developers Part 3

Java Language: Data Type In Java, We have premitive data types and objects. Java support 8 premitive data types for different purpose. Data Type Value byte 0 short 0 int 0 long 0L float 0.0f double 0.0d char ‘\u0000’ boolean false Reference: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html Go Language: Type Go language is statically typed programming language. It means that variable always has specific type that can’t be changed. Go language data type can be divided in main three categories.

Read more →