How to Convert Time to String in Golang

If you need to convert Time to a String in Go, then you can do one of the following: Option 1 – Using time.Now package main import ( "fmt" "time" ) func main() { currentTime := time.Now() fmt.Println("Time: ", currentTime.String()) } Option 2 – Using time.Time.String() package main import ( "fmt" "time" ) func main() { Time := time.Date(2022, 03, 28, 03, 50, 16, 0, time.UTC) t := Time.String() fmt.Printf("Time without the nano seconds: %v\n", t) }

October 6, 2022 · 1 min · 77 words · Andrew

How to Perform a Deep Copy in Golang

To perform a Deep Copy in Go, you can use a struct type as follows: Deep Copying using a struct in Go package main import ( "fmt" ) type Dog struct { age int name string friends []string } func main() { john := Dog{1, "Harry", []string{"Steve", "Matt", "Sarah"}} jack := john jack.friends = make([]string, len(john.friends)) copy(jack.friends, harry.friends) jack.friends = append(jay.friends, "Fred") fmt.Println(john) fmt.Println(jack) }

October 5, 2022 · 1 min · 65 words · Andrew

How to Return Lambda Functions in Golang

Go doesn’t typically have Lambda Expressions, but synonymous to Lambdas, or Closures if Anonymous Functions for Go. How to return a value from an Anonymous Function in Go package main import "fmt" func main() { var sum = func(n1, n2 int) int { sum := n1 + n2 return sum } result := sum(5, 3) fmt.Println("Sum is:", result) } How to return an Area from an Anonymous Function in Go package main import "fmt" var ( area = func(l int, b int) int { return l * b } ) func main() { fmt....

October 4, 2022 · 1 min · 96 words · Andrew

How to Create an Empty Slice in Golang

If you would like to create an empty slice in Go, then you can do the following: Option 1 – Initialize an Empty Slice in Go package main import "fmt" func main() { b := []string{} fmt.Println(b == nil) } Option 2 – Using make() package main import "fmt" func main() { c := make([]string, 0) fmt.Println(c == nil) }

October 3, 2022 · 1 min · 60 words · Andrew

[Solved] dial tcp: lookup proxy.golang.org: i/o timeout

If you get a timeout when trying to install go dependencies, the error may look something like this: $ go get github.com/aws/aws-sdk-go/aws go: module github.com/aws/aws-sdk-go/aws: Get "https://proxy.golang.org/github.com/aws/aws-sdk-go/aws/@v/list": dial tcp: lookup proxy.golang.org: i/o timeout How to Solve the Timeout Issue when installing Go Deps export GOPROXY=direct Then re-run your go get command.

September 25, 2022 · 1 min · 51 words · Andrew

How to Execute Linux Commands in Golang

If you want to execute linux commands in Golang, then you can use exec.Command: cmd := exec.Command("echo", "hello world") res, _ := cmd.CombinedOutput() fmt.Println(string(res))

July 4, 2022 · 1 min · 24 words · Andrew

How to Check for Prime Numbers using Golang

If you need to check for Prime Numbers in Golang, then you can use the following method: const n = 1212121 if big.NewInt(n).ProbablyPrime(0) { fmt.Println(n, "is prime") } else { fmt.Println(n, "is not prime") }

July 3, 2022 · 1 min · 35 words · Andrew

How to Raise a Number to a Power in Golang

If you need to raise a number to a power in Golang, then you can use the math.Pow function: package main import ( "math" ) func main() { var exponent, base float64 output := math.Pow(base, exponent) }

July 2, 2022 · 1 min · 37 words · Andrew

How to Calculate the Sum of the Numbers in the Nth row of a Triangle in Golang

The challenge Given the triangle of consecutive odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 ... Calculate the sum of the numbers in the nth row of this triangle (starting at index 1) e.g.: (Input –> Output) 1 --> 1 2 --> 3 + 5 = 8 The solution in Golang Option 1: package solution func RowSumOddNumbers(n int) int { return n * n * n } Option 2:...

May 24, 2022 · 2 min · 228 words · Andrew

How to SHA256 a String in Golang

If you need to SHA256 a String in Go, then you can use the crypto/sha256 package. SHA256 a String in Go package main import ( "crypto/sha256" "fmt" ) func main() { s := "A sample string to SHA256!" h := sha256.New() h.Write([]byte(s)) bs := h.Sum(nil) fmt.Println(s) fmt.Printf("%x\n", bs) } The output will look like this: A sample string to SHA256! 9abf637c7e39cc4ef84d6d92cf7ffe168dc922b8ae666260d907e0353865ce89

May 18, 2022 · 1 min · 61 words · Andrew