1. package main
    2. import (
    3. "fmt"
    4. "strings"
    5. )
    6. func testHasPrefix() {
    7. /**
    8. 判断前缀是否以xx开头
    9. */
    10. str := "http://baidu.com"
    11. if strings.HasPrefix(str,"http"){
    12. fmt.Println("str is http url")
    13. }else {
    14. fmt.Println("str is not http url")
    15. }
    16. }
    17. func testHasSuffix() {
    18. //判断是否以xx结束
    19. str := "http://baidu.com"
    20. if strings.HasSuffix(str,"baidu.com"){
    21. fmt.Println("str is baidu url")
    22. }else {
    23. fmt.Println("str is not baidu url")
    24. }
    25. }
    26. func main() {
    27. testHasPrefix()
    28. testHasSuffix()
    29. }

    输出:

    1. str is http url
    2. str is baidu url