golang: Get last character of a string

A very quick one here. I needed to get the last character of a string in golang, but thinking like a PHP developer I was looking for an equivalent to substr($var, -1);.

As it turns out, you actually do it using slices

newVal := val[len(val)-1:]

This says “start at string length, minus 1 and give me everything to the end”


Published:
November 25, 2014

Category
Development