Golang : Increment string example
Problem:
You need to create "copies" of a file, prevent user from overwriting files accidentally or need duplicate database content which has unique titles or slugs. Having increment string function is helpful in this kind of situation. How to do that?
Solution:
Increments a string by appending a number to it or increasing the number.
Below is a function to increment the string value that you can use or adapt to your own Golang code.
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func incrementString(str string, separator string, first int) string {
// set default values
// see https://www.socketloop.com/tutorials/golang-proper-way-to-set-function-argument-default-value
if separator == "" {
separator = "_"
}
if first == 0 || first < 0 {
first = 1
}
// test to see if str already has integer suffix(ends with _#)
test := strings.SplitN(str, separator, 2)
if len(test) >= 2 {
// increase file counter by 1
i, err := strconv.Atoi(test[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
increased := i + first
return test[0] + separator + strconv.Itoa(increased)
} else {
return str + separator + strconv.Itoa(first)
}
}
func main() {
result := incrementString("file", "_", 0)
fmt.Println(result)
result = incrementString("file", "-", 2)
fmt.Println(result)
// increase by 1
result = incrementString("file_2", "", 1)
fmt.Println(result)
// increase by 2
result = incrementString("file_2", "", 2)
fmt.Println(result)
// increase by 100
result = incrementString("file_3", "", 100)
fmt.Println(result)
// change separator to # sign
result = incrementString("imagefiles", "#", 10)
fmt.Println(result)
// will NOT accept negative number. will change to default value of 1
result = incrementString("imagefiles", "#", -99)
fmt.Println(result)
}
Output:
file_1
file-2
file_3
file_4
file_103
imagefiles#10
imagefiles#1
Happy coding!
References:
See also : Golang : How to check if a string starts or ends with certain characters or words?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.6k Golang : convert rune to unicode hexadecimal value and back to rune character
+6.5k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+6.2k Golang : Handling image beyond OpenCV video capture boundary
+22.4k Golang : simulate tail -f or read last line from log file example
+20k Android Studio : AlertDialog and EditText to get user string input example
+7.2k Golang : Convert source code to assembly language
+20.4k Golang : Convert date string to variants of time.Time type examples
+5.5k Linux : Disable and enable IPv4 forwarding
+8.7k Golang : Inject/embed Javascript before sending out to browser example
+8.2k Golang : Add text to image and get OpenCV's X, Y co-ordinates example