dongnigeng1295
2018-06-03 15:13
浏览 1.0k
已采纳

golang逗号分隔的表达式

In go docs there is a shuffle example (link)

I've added some output to it as follows:

package main

import (
    "fmt"
    "math/rand"
    "strings"
)

func main() {
    words := strings.Fields("ink runs from the corners of my mouth")
    fmt.Println(words)
    rand.Shuffle(len(words), func(i, j int) {
        fmt.Println("Before:", i, j, words[i])
        words[i], words[j] = words[j], words[i]
        fmt.Println("After:", i, j, words[i])

    })
    fmt.Println(words)
}

What I would like to understand is the line that has:

words[i], words[j] = words[j], words[i]

And specifically why the output before and after that statement is different. How is this evaluated? Thanks.

图片转代码服务由CSDN问答提供 功能建议

在go docs中,有一个洗牌示例(链接

我向其中添加了一些输出,如下所示:

  package main 
 
import(
“ fmt” 
“ math / rand” 
“ strings” 
)
 
func main(){
 words:= strings.Fields(“ ink 从我的嘴角跑“” 
 fmt.Println(words)
 rand.Shuffle(len(words),func(i,j int){
 fmt.Println(“ Before:”,i,j  ,单词[i])
单词[i],单词[j] =单词[j],单词[i] 
 fmt.Println(“ After:”,i,j,单词[i])
 \  n})
 fmt.Println(words)
} 
   
 
 

我想了解的是具有以下内容的行: \ n

  words [i],单词[j] =单词[j],words [i] 
   
 
 

,特别是为什么之前和之后的输出 之后的说法是不同的。 如何评估? 谢谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题