Empty Struct
All things Tech | Golang
by Maria DeSouza
3y ago
An empty struct in Go has no data elements. type S struct{} The most important property of the empty struct is that the width is zero. This allows to create a slice or channel of thousands of empty structs with a tiny memory footprint. Here is a size comparison of the empty struct vs empty interface vs bool: package main import ( "fmt" "unsafe" ) func main() { var s struct{} var i interface{} var b bool fmt.Println(unsafe.Sizeof(s), unsafe.Sizeof(i), unsafe.Sizeof(b)) } On a 32 bit system: 0 8 1 On a 64-bit system:0 16 1null Uses of empty struct As a method receive ..read more
Visit website
Golang Net HTTP Package
All things Tech | Golang
by Maria DeSouza
3y ago
Golang’s net/http package can be used to build a web server in a minutes. It packs in a pretty wide use of Golang concepts like functions, interfaces and types to achieve this. Here is a basic web server using Go: package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", handlerHelloWorld) http.ListenAndServe(":8082", nil) } func handlerHelloWorld(w http.ResponseWriter, r *http.Request){ fmt.Fprintf(w, "Hello world") } If we run the above server we can make a GET request and the server will print “Hello World”. What we need to understand that in the backgr ..read more
Visit website
API Performance Testing
All things Tech | Golang
by Maria DeSouza
3y ago
The goal of API Performance Tests are to conduct  load tests that will run broadly across all endpoints of an API and help us understand the distribution of throughput in requests per second – average, peak, etc. It is important to record response times and resource utilization at average and peak loads. This will allow us to determine system response times, network latency, etc. We should also be able to determine the concurrency and processing overhead of the API. We should measure performance when concurrent instances are instantiated with instructions to run load testing scripts. Tool ..read more
Visit website
Auto-generate code using Go templates
All things Tech | Golang
by Maria DeSouza
3y ago
Go has two template packages: a HTML template package that can be used to templatize HTML files and a more general text template. The Go text template package can be used easily to generate custom code like Javascript, Swift, React etc. https://golang.org/pkg/text/template/ This package can be very useful for building products for varied customers that are similar but require their own company logo, names, images and text branding. In fact, this can be implemented in any codebase that is very similar but needs a few tweaks to work for different platforms or products. I have used Go text templa ..read more
Visit website
Launch a Golang web server using Docker
All things Tech | Golang
by Maria DeSouza
3y ago
If you want to create a web server using Go the simplest way to deploy is using Docker.  Golang code is compiled to a binary and does not need a special environment to run. Here is the simplest web server code in Go to get started. Save this as webserver.go package main import ( "fmt" "log" "net/http" "runtime" ) func main() { http.HandleFunc("/", indexHandlerHelloWorld) log.Fatal(http.ListenAndServe(":8080", nil)) } func indexHandlerHelloWorld(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello world, I'm running search ..read more
Visit website
Packages on the Go
All things Tech | Golang
by Maria DeSouza
3y ago
Package management with Go is a very talked about issue. Unfortunately, go get does not support functionality to fetch specific tags or versions. It gets the package from the HEAD in git. Recently we had a situation where a developer on our team used a package and then it got obsoleted. The package developer tagged the release before making breaking changes and the sources were still available but we had to do a git checkout and run it. That is when I looked into package management. Go 1.5 introduced the “vendor” directory as an experiment and made it official  in Go 1.6 If you ..read more
Visit website
Pass by value or reference
All things Tech | Golang
by Maria DeSouza
3y ago
The official Go site FAQ states,  “As in all languages in the C family, everything in Go is passed by value”. This is because the function gets a copy of everything that is passed in. Is there such thing as pass by reference in Go? There are different views  as to what is exactly pass by reference to Go.  Some strongly maintain there is no such thing as pass by reference. In C++ terms, the actual meaning of pass by reference is you pass a reference or a pointer to the actual data structure rather the data itself. The function then can modify the value of the argument using that ..read more
Visit website
Bit manipulation in Go
All things Tech | Golang
by Maria DeSouza
3y ago
Bit manipulation is important to know when doing data compression, cryptography and optimization. Bitwise operations include AND, NOT, OR, XOR and bit shifts. Go supported bit operations include: & AND| OR^ XOR&^ AND NOT<< left-shift>> right-shift Go does not have a dedicated NOT operator like C++ or Python. Instead we have to use the XOR operator to toggle the bits. var n byte = 0x0F fmt.Printf("%08b\n", n) n = ^n fmt.Printf("%08b\n", n) This is the output: 0000111111110000 Here are some common bit manipulation algorithms that can be done in Go Swapping integ ..read more
Visit website
Reflecting on structs
All things Tech | Golang
by Maria DeSouza
3y ago
Reflection in Go allows  to manipulate objects and are most useful when dealing with structs. I always wished I could range over a struct. But “range” only supports builtin types such as string, list, and map. Reflect makes it easy to do so. I have a package that contains a function, I use frequently to do exactly this.  It takes in a struct and copies over the elements to a map so we can range over it.  https://github.com/mariadesouza/structutil Here is a small example: package main import ( "fmt" "github.com/mariadesouza/structutil" ) type testStruct struct { Name ..read more
Visit website
Mocking with Golang
All things Tech | Golang
by Maria DeSouza
3y ago
Overview Writing unit tests are vital and ensures the integrity of your code. But sometimes it is  hard to write tests for code that references external dependencies. That is where Go makes mocking such services easy. The core idea is to create the interface with one or more of the methods from the original package struct. For example, I wrote a wrapper SFTP package  to create SFTP connections and download/upload files. https://github.com/mariadesouza/sftphelper Inorder to test this package, rather than downloading and uploading files to an actual SFTP server, I used  mocking so ..read more
Visit website

Follow All things Tech | Golang on FeedSpot

Continue with Google
Continue with Apple
OR