go to achieve reversal of the string (reverse) - By the way, go syntax too cool

go language string reversal reverse method

package main
import "fmt"
func main() {
s := "foobar"
a := []rune(s)
fmt.Printf("%s\n", s)
fmt.Printf("字符串逆转过程:\n")
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\n", string(a)) 
}

Output is as follows:

C:\Users\asus\Desktop>test.exe
foobar
字符串逆转过程:
roobaf
raobof
raboof
raboof

It is worth saying is a[i],a[j] = a[j], a[i]go inside to call it parallel assignment. Such as:
a, b := 20, 16
the equivalent of, a = 20, b = 16 .

:= Variable declarations, assignment also awesome

goto 语句

Incidentally, note the use of the goto statement in mind

for i:=0;i<10;i++{
fmt.Printf("%d\n",i)
}

-------循环语句和goto分割线

i:=0
here:
fmt.Printf("%d\n",i)
i++
if i<10 {
goto here
}

They are the equivalent of two

Guess you like

Origin www.cnblogs.com/famine/p/11973534.html