• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Lpl
V2EX  ›  程序员

gopher 们写 for-range 的时候喜欢用 index 还是 object?

  •  
  •   Lpl ·
    penglongli · Aug 12, 2019 · 1331 views
    This topic created in 2491 days ago, the information mentioned may be changed or developed.

    类型 1

    for _, obj := range slice {
    	_ = obj.Name
    }
    

    类型 2

    for index := range slice {
    	_ = slice[index].Name
    }
    
    William911
        1
    William911  
       Aug 12, 2019 via iPhone
    根据需要
    buried
        2
    buried  
       Aug 12, 2019
    index 是引用 object 是拷贝
    看情况了就
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6126 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 02:51 · PVG 10:51 · LAX 19:51 · JFK 22:51
    ♥ Do have faith in what you're doing.