How to extract the end of a string (suffix) in Go

1 Answer

0 votes
package main

import (
    "fmt"
)

func getSuffix(inputStr string, suffixLength int) string {
    lengthOfSuffix := min(suffixLength, len(inputStr))
    startIndex := len(inputStr) - lengthOfSuffix
    
    return inputStr[startIndex:]
}

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}

func main() {
    inputStr := "Software Programmer"
    suffixLength := 5 // Change this value to get a different suffix length

    suffix := getSuffix(inputStr, suffixLength)

    fmt.Println("The suffix is:", suffix)
}



 
/*
run:
 
The suffix is: ammer
 
*/

 



answered May 1, 2025 by avibootz
...