Skip to content

Instantly share code, notes, and snippets.

View slicesDemo.go
func main() {
cricketer := []string{1:"Sachin", 2:"Ponting", 3: "Waugh", 4: "Zaheer", 5: "Waqar", 6:"Lee"}
batsman := cricketer[1:4]
bowler := cricketer[4:7]
fmt.Println(batsman)
fmt.Println(bowler)
cricketer = append(cricketer, "McGrath")
fmt.Println(bowler)
fmt.Println(cricketer)
View array.go
func main() {
var a [3]int = [3]int{1,2,3}
fmt.Println("Element at index is",a[1])
fmt.Println("Length of array is",len(a))
for i, v := range(a){
fmt.Printf("Index is %d, and value is %d \n",i,v)
}
q := [...]int{1, 2, 3}
View complex.go
package main
import "fmt"
func main(){
var x complex128 = complex(1, 2)
var y complex128 = complex(3, 4)
fmt.Println(x*y)
fmt.Println(real(x*y))
fmt.Println(imag(x*y))
View pointerDemo.go
func main(){
f := 100
x := &f
fmt.Printf(" value of f is %d and the address where f is stored %x \n", f, x);
*x = *x * 2
fmt.Printf(" value of f is %d and the address where f is stored %x \n", f, x);
}
View myFirstGoCode.go
package main
import "fmt"
const boilingF = 212.0
func main(){
var f = boilingF
var c = (f - 32) * 5/9;
fmt.Printf(" boiling point in Centigrade %g and in Fahrenheit %g \n", c, f);
View Main.java
package com.company;
import java.util.concurrent.*;
public class Main {
/**
* To book a parking, we need customer details and parking details.
* And we don't need any booking return object for the client, in other word,
* we can make booking process async/
View demoCancel.java
private static void demoCancel() throws InterruptedException, ExecutionException {
CompletableFuture<String> completableFutureFirst = CompletableFuture.supplyAsync(() -> {
System.out.println("Task Running inside completable Future");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "result";
});
View demoCompleteExceptionally.java
private static void demoCompleteExceptionally() throws InterruptedException, ExecutionException {
CompletableFuture<String> completableFutureFirst = CompletableFuture.supplyAsync(() -> {
System.out.println("Task Running inside completable Future");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "oops";
});
View demoCompletableFutureComplete.java
private static void demoCompletableFutureComplete() throws InterruptedException, ExecutionException {
CompletableFuture<String> completableFuture = new CompletableFuture<>();
//String value = completableFuture.get();
//The above statement will cause program to run forever.
// Because there is nothing for the completable future to do.
completableFuture.complete("Completing the completable future with this default text");
String value = completableFuture.get();
System.out.println("Value After Complete: "+value);
}
View demoThenCombine.java
private static void demoThenCombine() throws InterruptedException, ExecutionException {
long timeStampStart = System.currentTimeMillis();
CompletableFuture<String> completableFutureFirst = CompletableFuture.supplyAsync(() -> {
System.out.println("Task Running inside completable Future");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "one plus ";