douyan1921 2018-08-09 08:02
浏览 72
已采纳

在go中删除或调整数组大小

I need delete or resize isPrime array after CalRange func called (isPrime=nil not working)

isPrime := [size]bool{}
CalRange(size, maxİndex, isPrime[:])
isPrime = nil

Thanks all for answers I solve it like that. How to delete struct object in go?

person1 := &Person{name: "Name", age: 69}
// work with person1
// Clear person1:
person1 = nil
  • 写回答

2条回答 默认 最新

  • dpn68721 2018-08-09 08:08
    关注

    In Go, arrays are different from slices. Slices can be nil, but arrays can't, since they are static.

    In your code above, on the line 1, the contents of isPrime are actually [false false false ...] with as many false values as your size variable allows.

    Thus, your array can't really be deleted or resized. It can only be zero-valued.

    If you really need to resize it, I recommend using slices instead, or creating a new array with the new size and copying the previous array's contents into it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 nrf52810-c三个a 程序
  • ¥15 lego-loam跑出来的roll误差很大
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对