append is the go to function when you are adding an element to a slice. But is it the best way to do so?

Short answer, no it isn’t.

Here’s why…

First, let’s create two functions that adds or assigns "x" to a string slice.

  1. WithAppend calls append to add "x" to a string slice. This is probably the most straightforward method.
func WithAppend() []string {
var l []string
for i := 0; i < 100; i++ {
l = append(l, "x")
}

return l
}

2. WithAssignAlloc also creates a string slice by specifying the size with make


When you have an io.Reader , the most common way to read is with ioutil.ReadAll but it is not the best and most efficient way.

So here are three functions to compare the different methods to unmarshal JSON from an io.Reader

  1. With ioutil.ReadAll
func IOUtilReadAll(reader io.Reader) (map[string]interface{}, error) {
var (
m map[string]interface{}
b, _ = ioutil.ReadAll(reader)
)

return m, json.Unmarshal(b, &m)
}

2. With io.Copy

func IOCopy(reader io.Reader) (map[string]interface{}, error) {
var (
m map[string]interface{}
buf bytes.Buffer
_, _ = io.Copy(&buf, reader)
)

return m, json.Unmarshal(buf.Bytes(), &m)
}

3. With json.Decoder

func JsonDecoder(reader io.Reader) (map[string]interface{}, error) {…


I am sure you have wondered if switch is more efficient than if-else, or vice versa. So here is a little experiment to compare them.

  1. If-else
func If(i int) bool {
if i == 1 {
return true
} else {
return false
}
}

2. Switch with no condition

func Switch(i int) bool {
switch {
case i == 1:
return true
default:
return false
}
}

3. Switch with constants

func SwitchConst(i int) bool {
switch i {
case 1:
return true
default:
return false
}
}

And here are the benchmarks…

BenchmarkIf
BenchmarkIf-8…


I think we can all agree that we did not enjoy writing unit tests when we first started coding. We want to write for new features and maybe some occasional bug fixes.

Then comes the day when we need to refactor or make some improvements… and BOOM!

Unit testing in Go is easy and there is even a built-intesting package for writing unit tests.

// foobar.go
package foobar
func DoSomething() string {
// do something amazing here
}

To create a test file in Go, simply append _test to the corresponding file.

So let’s create a file called foobar_test.go which…


Have you ever wonder if you are concatenating strings efficiently? I did, and so here are the results.

But first, let’s discuss on three ways to concatenate strings.

1. Concatenation using +

The easiest and cleanest way to concatenate strings is to use + .

foobar := "foo" + "bar"

2. Concatenation using strings.Builder

Another way is with strings.Builder . As compared to the first method, this method is much longer and code readability might be affected.

var sb strings.Builder
sb.WriteString("foo")
sb.WriteString("bar")
// sb.String()

3. Concatenation using bytes.Buffer

Last method is with bytes.Buffer . Similar to the second method, it requires at least three lines of code.

var bb bytes.Buffer
bb.WriteString("foo")
bb.WriteString("bar")


Are you getting permission errors when you update WordPress with SELinux enabled? And facing similar errors you when you install plugins like W3 Total Cache?

You are not alone!

I had experienced this problem some time ago and it took me several hours to realize that it is due to SELinux. But don’t be tempted to disable SELinux completely.

In this article, I will show you how you can update WordPress with SELinux enabled.

What is SELinux?

SELinux, also known as Security-Enhanced Linux, is the bane of developers running web applications like WordPress on CentOS. But SELinux is there for a reason. It…


Speed to market is the key to success and is possible with serverless backend. With serverless technologies, you will not need to worry about managing your servers. You will be able to dive straight into implementation.

Most cloud providers support Function as a Service, or FaaS, and it allows you to build a serverless backend in matters of minutes.

Here are some of the advantages and disadvantages of FaaS.

Advantages of FaaS

It is cheap, really

An AWS Lambda costs as little as $0.20 per million requests and $0.0000166667 for every GB-second. This means you can build a microservice without burning a hole in your pocket.

Highly scalable

Every…


What are AWS Lambda and API Gateway

AWS Lambda lets you run your code without provisioning or managing any servers. This gives you a completely serverless architecture with little to no effort on maintenance.

Amazon API Gateway is a fully managed service for managing your APIs. In order for your Lambda to run, it will need a trigger event. AWS allows you to attach multiple different event sources, like SQS, DynamoDB Streams, but in this article, I will focus on API Gateway.

By setting API Gateway as the event source, Lambda can receive your API input and send the output back to API Gateway.

What is AWS CloudFormation

AWS CloudFormation allows…

Daryl Ng

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store