当前位置:网站首页>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"
边栏推荐
- Green Tao theorem (4): energy increment method
- Sword finger offer Second Edition: string (simple)
- js 对象数组去重
- Protocol buffers 的问题和滥用
- Cluster chat server: chatservice business layer
- NLP field history most complete must read classic papers classification, sorting and sharing (with Chinese analysis)
- Protocol buffers 的问题和滥用
- 开源分布式链路追踪对比
- Be a professional software craftsman
- Minimum spanning tree: Kruskal
猜你喜欢

Improving performance with explicit rendering

合宙ESP32C3硬件配置信息串口打印输出

flink原理及开发总结(详细)

WinDbg practice -- Introduction

VLAN comprehensive experiment

Problems and abuse of protocol buffers

Modular development

Edge cloud | 1. overview

Unity - 3D mathematics -vector3

Deep learning - NLP classic papers, courses, papers and other resources sorting and sharing
随机推荐
Yushu A1 robot dog gesture control
How to get the worker's hat? Where is the worker's helmet?
集群聊天服务器:工程目录的创建
Broadcast (broadcast)
C——文件
集群聊天服务器为什么引入负载均衡器
Minimum spanning tree: prim
Still have 1 requests outstanding when connection from slaveX/X.X.X.X:33202 is closed
Summary of database stress testing methods
TCP half connection queue and full connection queue (the most complete in History)
Looking for the missing class name
SQLite database
Is it safe to open a securities account online?
Cookie 和 Session
Be a professional software craftsman
Deep learning - NLP classic papers, courses, papers and other resources sorting and sharing
Uncertainty of distributed energy - wind speed test (realized by matlab code)
Boost Filesystem使用手册
Use Gaode map JS API 2.0 to load the starting and ending path tracks
C - documents