Go lang
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
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.
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.
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.
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.
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.
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
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.
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.
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.
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.