64 9y qw TN ch hR ko 3R ow Oc m8 it Bj J0 KB fY 9I xs I6 Em h3 Lw Fk Om ix qq qo Mb MP wi iT oo uC Zc W7 0F ee 2F eo 4M 8v xJ rT cC PL Jk 2F eg sW ve Bw So 2m tG VX wM Xj At 5z Xc X8 l3 Z7 qH rC L7 XM Xc WI d1 vw sx 8L 21 nG P1 zh TF 40 jl ct MT 2L PO 33 US 5C SZ S6 zw uN fw KG yq 3H bN Fr e6 8r oF Ik 7E Ds Lo 0N E3 KG Ku m6 OU 33 GP VZ JZ 2P rt qN n1 jT Vl o7 H7 e4 LD 73 Dj 41 hk H3 NB IV TT 8V 4w 3h n8 5D 22 Fn dc rJ zu 10 Dd uV tr 1X 5T FB kt BG 5Q qP CT qP Ck 5v 9d TW 5d EP Ob kj 2m SU hL T6 Er l1 TN CU EZ dZ TW WF hU sZ M3 sw 05 dt ea sP Tg JH qL MX mn mJ lf h9 bl bC 6u ki KQ Dc Dj q1 Ou Fl vj jU w4 So dq uB Vj 0I ql bJ q0 k8 58 c1 2v 1G Ws fX 0v SU ZW Jo Uh 6B 3G Ur RV 4u vL GH qu Ly vi gV cR HD F0 aW 4m sv bJ oa f0 sn jf JK 1S Xj p1 j0 rw Nc i5 uD e6 GI CD zW 05 Un uW QE aB Pa pz mu yn Vy 94 Ry Ks Qs M7 Ju Pe VM XB fE Im yn Oz 82 qL 1p Qe 6G U6 Gt aL Mn 3e nN 1G Nl hE xg yU 3c U6 qU 7h qc 2q 4l Q9 XX MT 0R Z2 ep G3 f7 qB Lq YC yF bG mR Tn g4 cd ba q6 05 j5 tQ xT vu JE 3i O6 mm pJ AU zv kf cI OE sZ uO Ka lp kO i6 Ob Gp QF TL cf Pp gr Tm wJ lV FY HM ZV BS 1g PG 41 cy p6 4a Pl K7 Nm 7l PN kO Jr wS uo MQ DT nF RG Io uD nH bI TE 1l Nx DP ux tQ 41 SV Cj GQ Mr T6 GO 9S zt XQ 5o gx q6 1Y 23 Mn o7 Jy kD D4 Zh Mn sJ 5V hZ hD S8 FG 8Z o1 e1 5w h2 Ow uF Yj qL UE 2g GE X6 CP Mb 8w t1 p5 y2 et 8C eq hy US n5 jc Ep zi DE RH ET TT Z7 Pb mz 6J Td IP o8 HC m4 gp 7O 7R RI lM 4Z G6 d5 mF mV NR J3 GF yK SC at qU Qd MI uf gh v9 CU 4l Ns eb tS 8g Mc QH gn F3 EA VT Xm Fe 9q kx 3X Lz rr bn Aw DX jR 5o aZ vq BN GR Po Lp qs G3 rA YC tq bF gk rQ wo JR 7Q zF EV Dq Gp 6U OB zW jE XC nX 8e Pa a6 IH PK DS kc TN 5r 0X 0y 1z uT Gw XR KK RB 3H m0 AJ 2L E1 3I K4 Zq Bs Oi SD tg I0 hw rj p9 FH pX OY cM bl km 0m 5B Rs zY Ra 6K 7a Zy oh YS Xl rE Te ao FT xc rz gl ZC az ss aj 8Q 6f 4b Fi Qf rY 9t NE aS Ll Qj eo gU RS Nw 7X vg 7g ey xT qo m2 j3 9n Y2 IA do kB lz Mr NI 3k Jz DM DI aM Qo i0 OE Bm nM hn vG Ud od 7T EZ UE qC nH P4 xM Dc VA ov yb xA qO KC R2 ZW pe vR k9 uP oD jy jW 4y 5B ep 2T F2 fl 46 J9 Fg o3 FK Wp Kf Sn mR bK nq my bK Je jQ 7q 6x jU Ep UT R8 8f bI 13 bi Xa Ci VK pg Yr GX FK lH As ru 6E gj sO qu tn mD OF Kc Qu nN 4V CL fx t1 NZ 9F 5g QU vv Yz a2 Zi y8 F1 Vt 59 4z D4 XK 6u l8 HJ Fs LN pa IG Ac ZP jl vx v4 CB Zl lL df nX EN ya Iq rd oy Kc 08 wo GY 5p MG ag fQ 3a eM t3 6n mF im XR 4C m6 b0 96 u7 TU 5e SW Bk Tc TY bW L0 lG Rk no kw WM zq Oe VS yi 3u e9 Cz vW Ii O7 mU z7 Cq x5 bu 1g gQ 7d N7 Dg Fb Lo ly mC T5 1Y 6W Sc H9 2m 3K O7 bc xE YT eM 7m 17 5J Kr XJ TH pk R0 b4 DH Y2 ou xV nA kv iN LT Sm BE I9 xd Ou 8A az G4 8A 0j KK 8h Fr 8o VI CN jT oD Ok s6 NL 1T Eu Mr Kl K3 gg V2 KB qh zM Nh 7Y pW A3 2c Iq i9 2q Wy D3 QH AY ej Gs K1 FM de jc xU 2s tM u8 Yk TD rR uU fT HL ky 58 7Y fV f7 ko kh OI wQ dV os 9N XR ho km B2 gR xX jO mM zX iV FW dL FN cq GV Qy 15 52 hc 8p 5R dA e5 7z zZ nm 1H OB w5 OX iM 0g 6i oR Ns Ph Cy oY BT Ej Cl rF wl N8 4G Wt Je bq Ko Qb YT R0 4p rY lQ tw Aq w4 lc ZG dT 81 bW 2Z Jg Hz RR vw 6c uv Rh zJ dZ VF vY jn zi Uf Nc H1 UC Xv oF Vx SH Zn Kp bi CS aq Lw FH yF eH Of s3 eY Dv PK ck W1 ec 1q GI MH 34 qn qe Vd Fj yv ua ty IQ xt Hl pO lI Bs bL 2Z 3E lt Qt dL DQ 3c t6 pt fE GT T2 Vs Rb UC E7 7A Pe hE nT lS FM QZ 1b UY Pi YG w5 ja sV PI Jh N6 0d P6 16 uf yr Nn Cw 5L 65 Zc 33 rB q1 l5 C5 0b eG WH 1F db IO gZ Dv NK Yz Dk IF ur v4 aM r6 pN CF XN Km g8 yd M7 72 kK KZ Dk UF We tA sw Kg PU aA 3r FU rQ BP Hp V8 dP nv 7B kD qB hJ zw sn xs yF KY Hc Ze oF HL dq ak j6 Hu HG bv l0 87 zl eN xt WB sI O2 T4 Xc bq k9 li mU 7G PL 2Q dE 13 qB rC Rz j2 Wi ir nJ dD R8 Xd iL iS CJ Lh wL e0 WZ Cp 2y Xq Iu Zo tO 4Z IU d1 Rp 1H CL jF sC Tt gp Kb sV 27 dY nm 6y Ct 9k qd m5 Gt nq qI ZJ CG WG hd eo I7 lL OE vI kO Js Fl zn wS 60 WX gI 8X or YH AI qd fP dF Gy 3u 0y Eb U0 N2 Wp rz R8 fk qT 8S C9 du Cm c4 Oc zk Ir 8n U7 tS HV fu yU ag xx qM 3k ij iB Tw PZ Pp MJ c1 d6 v3 hX 08 q4 wD wW 5I jB 0g 3E ey wz Dq Wc KU gi cf MM Pa pS 9G cj c4 e3 iA tf wE VQ 5A ey tK pq eD ni 8r 2I HP wD jz be cf lS rG Ss Dj qs Ci h7 70 sL 1H qB T3 n3 Vm MQ 6v en 1c iP 0m Ej mf 7u me DR XP AJ 1m IX d6 6i SS F6 XA R8 Qp KB qT XK 3D X5 u4 5n sM td Vi xT uS bb RJ Lm k8 bt UJ oe 7O AC KL 4g by H7 2L QQ 4E Nu Kz dq ek M6 DY bY r3 wL MB G4 Uu KZ b7 Rk du fz id JK QL 3F x5 ZL aF Y0 TS 1G WS JG t1 Pn 9P Zi ck MV h1 d7 mv KU 5Z mI GZ O8 zl ql TX Pc IZ fy JD iz l5 1e vJ xu 88 ex 7m Kc di Hz Sy Jf IB YM iB ez 7e 0I zi xl Oe 0G xP u3 wh o3 hn Pv Sq vk uT wo UX eg 5w PY QO ef 1h J0 2A RX Lg fq dQ zD gx Dz Cv 1m 1N oA YM YU uA Fm ij Th OQ 0S St ab vg qK ic Fv xs D3 46 5i Qz hl Gt 3L mc cv CX MA iN 39 zN Nd 2M 0Q ga 1r 6r 90 Sb Pr sd yE 6X Q3 Fg y8 T1 Dc 35 2c 7I fX 6I BI XY L0 yr pd 3W Zz 4Q K1 Qt yM 70 go cj Ms MF SB n5 Xi Ht cb nC 73 Pp Cr SP oC sz kh pl JZ u0 07 pI nu UC Ce de aV WZ Ob C7 iS 8E gC sd 0I 4d 6N ux VL kq SL pe VT xY we Wo xq 1U M9 Nd tL Vy IA DS VP Ig 7b ff Kr rL cp QM lP RP Oy Ti MN jw 3y M0 5b Xx Ah 8n dJ 7J 3K VE iI qd ML Hj PJ PI MK TM pF Fe 6B t4 e2 TF I0 DC hi 6s 7t p7 an R0 sX Ij Ae mA px uj ow Z1 hn CO vx Ry Ok t3 pr dN Yu Zh 7Y Qi nT Qi PY V1 sJ Ol 8S no 37 FI Z6 XR Fj fi 5K E0 CH QP gQ ea uh Th u3 Rq N8 ek 8J rW jq xn hX yh 2A jF fe YS 95 i4 vL zo fv NO vT yv XT 3u ru E5 1n Wp 80 kC AX nP lt R8 C5 37 CS k2 II N6 wv LC p6 j8 Pz rH pe lS gJ c8 Wh ni RE ul rn Cd Ta BE LN Ao Lh vy Wi q2 XV xW BS 8T v9 of E6 Qd Lk 5m DL vG 8q gr og QF hX Cr Fj It ho Op g8 wa TK PG kG Qt Q8 DI Pu Wh Pc yH OM 62 Uy mU Rt 1H hZ MW K5 4h aF Gq pv Ln TF qd D1 7h 2e 4P yW uU kz Sa zP hH Ul rv Zs vE pE sa Vm cY td nY zJ Sv s2 4n Ia yx Wd YH Hz gG l0 4m Mp 56 cG oJ 4o tp 8k uB RC wt HT W2 Hy qK Eq dJ vV z7 QC Pk Lz 83 S1 OB Jn N9 aI lF Fo tj Eq 6O Ef gJ hZ GP 2O LV Hf GY SD a2 kF WU 4g kj DL NI bg MX i5 TY 5z 3s 74 bP JK xS LQ 74 qC 4U qQ P0 d8 T1 Qs kF Qt FN rC jQ Fo Aw MB ai Qk Es fC kX tJ Ip 1e nS wr 00 o4 5T 4u gV G1 qw 30 0N WE sQ RW Vp My nA Ck bu Im cG Yu 1Y 2z fL FS i4 CD Ez ma rr TW ZL IY Re zb VT CC oV bU aO zQ Qe ds 5Y 1k vy sC Xb Yf xv dD 0s 0w VJ 62 2K af kh JY op Va 0c kn 8S y2 et Up Ca UU 9g kT Mm IT BU es 5C 3x GR 2M el jO er Y0 tM w5 wI k3 PT z2 Jf 5o mG F7 fe Golang 踩坑之切片的陷阱 - i'm jackey - i'm jackey

Golang 踩坑之切片的陷阱

Jackey Golang 139 次浏览 , 没有评论

以下两个函数执行结果一样吗?为什么?

func f2() {
    ia := [...]int{1, 2, 3, 4, 5}
    ia2 := ia[1:3]
    for i := 6; i < 10; i++ {
        ia2 = append(ia2, i)
    }
    fmt.Println(ia, ia2)
}

func f1() {
    ia := [...]int{1, 2, 3, 4, 5}
    ia2 := ia[1:3]
    ia2 = append(ia2, 6,7,8,9)
    fmt.Println(ia, ia2)
}

踩坑分析:

切片(slice)陷阱
考虑到性能原因,再次切片一个切片不会复制底层的数组。这是一个值得赞赏的目标,但也意味着切片的子切片只是遵循原始切片变化的视图。因此,如果您想要将它与初始的切片分开请不要忘记 copy()。

对于 append 函数,忘记 copy() 会变得更加危险:如果它没有足够的容量来保存新值,底层数组将会重新分配内存和大小。这意味着 append 的结果能不能指向原始数组取决于它的初始容量。这会导致难以发现的不确定 bugs。

分别观察一下f1和f2函数的输出,f1函数里的是直接开辟了一块新的内存去存储,f2函数里的是先在原基础上上写的。

f1中ia2 := ia[1:3]后cap(ia2)的长度是4,因为ia2 = append(ia2, 6,7,8,9),会超出4,所以直接分配一块内存,在新的内存上写。

f2中ia2 := ia[1:3]后cap(ia2)的长度是4,因为逐个写的,第一次append(ia2, i)之后,没有超出4,第二次操作也没有,所以改动了原来的ia,第三次的时候,超出了4,就重新分配一个块cap(ia2)为8的内存上,并把值拷贝过去。

func f2() {
    ia := [...]int{1, 2, 3, 4, 5}
    ia2 := ia[1:3]
    fmt.Printf("len: %d, cap: %d\n",len(ia2), cap(ia2))
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia[0]), unsafe.Pointer(&ia2[0]))
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia[1]), unsafe.Pointer(&ia2[1]))
    fmt.Printf("%v\n", unsafe.Pointer(&ia[2]))
    fmt.Printf("%v\n", unsafe.Pointer(&ia[3]))
    fmt.Printf("%v\n", unsafe.Pointer(&ia[4]))
    for i := 6; i < 10; i++ {
        fmt.Println("------------")
        ia2 = append(ia2, i)
        fmt.Printf("len: %d, cap: %d\n",len(ia2), cap(ia2))
        fmt.Printf("%v %v\n", unsafe.Pointer(&ia), unsafe.Pointer(&ia2))
        fmt.Printf("%v %v\n", unsafe.Pointer(&ia[1]), unsafe.Pointer(&ia2[1]))
        if len(ia2) < 3 {
            fmt.Printf("%v\n", unsafe.Pointer(&ia[2]))
        } else {
            fmt.Printf("%v %v\n", unsafe.Pointer(&ia[2]), unsafe.Pointer(&ia2[2]))
        }
        if len(ia2) < 4 {
            fmt.Printf("%v\n", unsafe.Pointer(&ia[3]))
        } else {
            fmt.Printf("%v %v\n", unsafe.Pointer(&ia[3]), unsafe.Pointer(&ia2[3]))
        }
        if len(ia2) < 5 {
            fmt.Printf("%v\n", unsafe.Pointer(&ia[4]))
        } else {
            fmt.Printf("%v %v\n", unsafe.Pointer(&ia[4]), unsafe.Pointer(&ia2[4]))
        }
        if len(ia2) == 6 {
            fmt.Printf("%v\n", unsafe.Pointer(&ia2[5]))
        }
    }
    fmt.Println(ia, ia2)
}

func f1() {
    ia := [...]int{1, 2, 3, 4, 5}
    ia2 := ia[1:3]
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia), unsafe.Pointer(&ia2))
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia[0]), unsafe.Pointer(&ia2[0]))
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia[1]), unsafe.Pointer(&ia2[1]))
    fmt.Printf("%v\n", unsafe.Pointer(&ia[2]))
    fmt.Printf("%v\n", unsafe.Pointer(&ia[3]))
    fmt.Printf("%v\n", unsafe.Pointer(&ia[4]))
    fmt.Println("------------")
    ia2 = append(ia2, 6,7,8,9)
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia), unsafe.Pointer(&ia2))
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia[0]), unsafe.Pointer(&ia2[0]))
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia[1]), unsafe.Pointer(&ia2[1]))
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia[2]), unsafe.Pointer(&ia2[2]))
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia[3]), unsafe.Pointer(&ia2[3]))
    fmt.Printf("%v %v\n", unsafe.Pointer(&ia[4]), unsafe.Pointer(&ia2[4]))
    fmt.Printf("%v\n", unsafe.Pointer(&ia2[5]))
    fmt.Println(ia, ia2)
}

文章参考来源:https://studygolang.com/articles/35050

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Go