登录
首页 >  Golang >  Go问答

续写函数导致输出结果异常

来源:stackoverflow

时间:2024-02-08 14:12:13 347浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《续写函数导致输出结果异常》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

func main() {
    x := []int{}
    x = append(x, 0)
    x = append(x, 1)
    x = append(x, 2)
    y := append(x, 3)
    z := append(x, 4)
    fmt.Println(y, z)
}

为什么输出是这样的? [0 1 2 4] [0 1 2 4] 而不是这个? [0 1 2 3] [0 1 2 4]


正确答案


这将实现您的预​​期输出[0 1 2 3] [0 1 2 4]

package main

import "fmt"

func main() {
    x := []int{}
    x = append(x, 0)
    x = append(x, 1)
    x = append(x, 2)
    y := []int{}
    z := []int{}
    y = append(y, x...)
    z = append(z, x...)
    y = append(y, 3)
    z = append(z, 4)
    fmt.Println(y, z)
}

终于介绍完啦!小伙伴们,这篇关于《续写函数导致输出结果异常》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>