zoukankan      html  css  js  c++  java
  • go实现字符串的逆转(reverse)——顺便说,go的语法太爽了

    go语言字符串逆转 reverse 方法

    package main
    import "fmt"
    func main() {
    s := "foobar"
    a := []rune(s)
    fmt.Printf("%s
    ", s)
    fmt.Printf("字符串逆转过程:
    ")
    for i,j := 0, len(a)-1 ; i < j ; i, j = i+1, j-1 {
    a[i],a[j] = a[j], a[i]
    fmt.Println(string(a))
    
    }
    fmt.Printf("%s
    ", string(a)) 
    }
    

    输出如下:

    C:UsersasusDesktop>test.exe
    foobar
    字符串逆转过程:
    roobaf
    raobof
    raboof
    raboof
    

    值得一说的是 a[i],a[j] = a[j], a[i] go里面把它叫做平行赋值。如:
    a, b := 20, 16
    相当于,a=20, b=16.

    := 的变量声明、赋值也超赞

    goto 语句

    顺便记记goto语句的用法

    for i:=0;i<10;i++{
    fmt.Printf("%d
    ",i)
    }
    
    -------循环语句和goto分割线
    
    i:=0
    here:
    fmt.Printf("%d
    ",i)
    i++
    if i<10 {
    goto here
    }
    

    它们两是等价的

  • 相关阅读:
    Git
    Spring
    Linux
    Linux
    Linux
    Linux
    Linux
    Linux
    Linux
    Linux
  • 原文地址:https://www.cnblogs.com/famine/p/11973534.html
Copyright © 2011-2022 走看看