当前位置:网站首页>Golang invalid argument to intn报错的解决
Golang invalid argument to intn报错的解决
2022-07-23 21:40:00 【CmderQ】
一 问题背景
最近的项目中,因为要处理随机的问题,所以使用到随机数。项目在测试的过程中,突然报错了:invalid argument to intn; 提示如下:

二 问题产生的原因
我这边使用的随机函数,是golang内置的包rand,使用的方法是rand.Intn,这个方法入参是一个非负数;然后返回值是[0,n)之间的随机数;我这边在使用的时候,未对入参n进行判断,在某些情况下,入参n会变成0,所以才导致上述报错:invalid argument to intn
事实上,这个报错是随机函数的源码实现中的提示;当传入的参数小于等于0时,就会抛出这个提示;实例demo如下:
package main
import (
"fmt"
"math/rand"
边栏推荐
- Cluster chat server: chatservice business layer
- C - documents
- 集群聊天服务器:网络模块ChatServer
- Pay more attention to which securities company has the lowest commission? Is it safe to open an account online?
- CMake的学习
- MySql的DDL和DML和DQL的基本语法
- Cesium core class viewer viewer details
- Failed to introspect class feignclientfactorybean exception troubleshooting
- 集群聊天服务器:工程目录的创建
- 2022.7.22 JS object
猜你喜欢

Unity solves that animation is not available: the animationclip 'xxx' used by the animation component 'xxx' must be marked as legacy

Protocol buffers 的问题和滥用

Union and union all of Hana SQL

High numbers | calculation of double integral 2 | high numbers | handwritten notes

Day109.尚医通:集成Nacos、医院列表、下拉列表查询、医院上线功能、医院详情查询

Qt桌面白板工具其一(解决曲线不平滑的问题——贝塞尔曲线)

集群聊天服务器:Model数据层的框架设计和数据库代码的封装

High numbers | calculation of double integral 3 | high numbers | handwritten notes

实验设计

Openlayers instance advanced view positioning advanced view positioning
随机推荐
面试快速复习(三):概率论与数理统计
flink原理及开发总结(详细)
LeetCode_376_摆动序列
Proof of green Tao theorem (1): preparation, notation and Gowers norm
scala编程(初级)
SQLite database
Scala programming (intermediate advanced experimental application)
集群聊天服务器:集群与分布式理论
如何在 pyqt 中实现桌面歌词
Use Gaode map JS API 2.0 to load the starting and ending path tracks
Postgraduate entrance examination | advanced mathematics Chapter4 indefinite integral
Several methods of obtaining longitude and latitude by cesium
寻找消失的类名
[create birthday card application]
Union and union all of Hana SQL
1309_ Add GPIO flip on STM32F103 and schedule test with FreeRTOS
分布式能源的不确定性——风速测试(Matlab代码实现)
Cookie 和 Session
js 对象数组去重
手机测试相关基础知识