Go语言中文网 为您找到相关结果 38

go语言实现遍历目录,及查找特定的文件类型

1 // filelist.go 2 package main 3 4 import ( 5 //"flag" 6 "fmt" 7 "os" 8 "path/filepath" 9 "strings" 10 ) 11 12 var ( 13 ostype = os.Getenv("GOOS") // 获取系统类型 14 ) 15 16 var listfile []string //获取文件列表 17 18 func Listfunc(path string, f os.FileInfo, err error) error { 19 var strRet string 20 strRet, _ = os.Getwd() 21 //ostype := os.Getenv("GOOS") // ...阅读全文

博文 2014-10-04 19:26:45 sn-dnv-aps

golang run时报undefined错误

问题现象:同一个文件夹下面有多个go文件,a.go,b.go,c.go,其中main在a.go中,直接go run a.go,报undefined 错误12原因:go在run之前会先进行编译操作,而在此处的编译它只会以这个a.go为准,导致其他几个引用文件中的方法出现找不到的情况 (而采用go build的方式又不一样,他会自动查找引用文件并打包)123解决方法:go run a.go b.go c.go 或go run *.g...阅读全文

博文 2018-07-13 15:35:23 hgqxjj

Golang 启动项目的时候遇到 cannot load。。。。,malformed module path 。。。missing dot in first path element的问题

省略的是项目中的子包名
版本:go 1.13
goland版本:2020.1.1网上查了一下,大多说的是go 1.13版本之后要注意import的域名规范,但是项目一直用的是go 1.13,发现这个问题也是在换了goland版本之后出现的事情而且在编译的时候,我注意到每次都会查找一个包,这在之前是没有的,怀疑是gomod没有关闭加上goland某些默认设置导致会在编译的时候查找依赖,解决方案:1. 查找goland有无相关配置可以关闭依赖查找 这一步大致找过没有找到2. 尝试关闭gomod- 关闭go mod- 重启goland,打开goland终端输入go env确认是否关闭- 再次尝试编译,这时候通过编译,没有报错总结一下,GO111MODULE=on: 也就是gomod开启...阅读全文

博文 2020-04-27 19:33:01 猫南北_6eb5

golang 多线程查找文件内容

1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 ) 10 11 var matchCount = 0 12 var ch = make(chan int, 512) 13 14 func findFile(path string, text string) { 15 var pathArray [100]string 16 var count = 0 17 filepath.Walk(path, func(path string, info os.FileInfo, err error) error { 18 if err != nil { 19 } 20 //f...阅读全文

博文 2015-02-28 03:00:01 modprobe

Golang http.DefaultClient 连接服务端压测时出现大量TIME_WAIT异常

今天在做一个压测场景时,通过golang作为client去连接服务,在client的服务器上出现大量的TIME_WAIT连接。经过查找资料,得到解决,参考如下: The default http.Transport is opening and closing connections too quickly. Since all connections are to the same host:port combination, you need to increase MaxIdleConnsPerHost to match your value for num_coroutines. Otherwise, the transport will frequently close the e...阅读全文

博文 2017-09-15 18:33:10 fdhay

二分查找法(Golang版本)

一组数据要进行二分查找,那么这个要查找的元素是有序,并且是连续存放(数组)。这样才可以进行二分查找。 下面首先来创建一个文件和数组 package main import ( "fmt" "math" ) type DataStruct struct { Data []int } func main() { a1 := DataStruct{[]int{1, 2, 5, 7, 15, 25, 30, 36, 39, 51, 67, 78, 80, 82, 85, 91, 92, 97}} fmt.Println(a1) } 数组a1是一个从小到大的有序数组,总共有18个元素 假设说我要查找30这个值,如果按照循环的查找方法,找到30这个值要执行7次。那么如果是按照二分查找呢?好吧,二分查找的...阅读全文

博文 2017-05-08 03:00:28 Zorn

关于Google Protocol Buffer的中文资料

最近打算写一篇关于goprotobuf的使用教程,先了解了Google Protocol Buffer相关的知识点。下面是谷歌到的不错的资料,故记录下来方便以后查找。 Google Protocol Buffer 的使用和原理:http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html Google Protocol Buffers浅析系列:http://www.cnblogs.com/royenhome/archive/2010/10/29/1864860.htm...阅读全文

博文 2014-10-09 16:51:51 Rhino(犀牛)

golang中os/user包用法

os/user包允许用户账号通过用户名或者用户id查找用户 type UnknownUserError type UnknownUserError string func (e UnknownUserError) Error() string //当通过lookup无法查找到某个用户时,便会返回该错误.type UnknownUserIdError type UnknownUserIdError int func (e UnknownUserIdError) Error() string ////当通过lookup无法查找到某个用户id时,便会返回该错误. type User type User struct { Uid string // user id Gid string // pri...阅读全文

博文 2015-06-18 17:05:37 chenbaoke

golang 类型转换

仅作记录,以便以后用到时查看方便。 字符串与整形: //string to 10进制int64 i,err := strconv.ParseInt(string, 10, 64) //int64 to string fmt.Sprintf("%d",int64)//我一般用这种方式 s := strconv.FormatInt(int64,10)//查找资料后得知这种方式比较正规 //string to int i, err := strconv.Atoi(string) //int to string s := strconv.Itoa(int...阅读全文

博文 2016-04-11 21:00:02 u013401219

golang binarySearch

1 func binarySearch(nodes []*node, word Text) (int, bool) { 2 start := 0 3 end := len(nodes) - 1 4 5 // 特例: 6 if len(nodes) == 0 { 7 // 当slice为空时,插入第一位置 8 return 0, false 9 } 10 compareWithFirstWord := bytes.Compare(word, nodes[0].word) 11 if compareWithFirstWord < 0 { 12 // 当要查找的元素小于首元素时,插入第一位置 13 return 0, false 14 } else if compareWithFirstWord ...阅读全文

博文 2015-03-28 03:00:03 rojas

Golang 字符串转URLCode

Golang 字符串转URLCode 最近因调用gitlab API,在生成某些字符串的时候直接请求 gitlab API 失败, url如下: keysURL := "http://192.168.1.212:10080/api/v3/user/keys?id=" + strconv.Itoa(model.Id) + "&private_token=" + privateToken + "&key=" + pub + "&title=gitclone" 查找问题发现是字符串编码问题,利用 url.QueryEscape() keysURL := "http://192.168.1.212:10080/api/v3/user/keys?id=" + strconv.Itoa(model.Id...阅读全文

博文 2018-01-17 00:30:00 Ljohn

golang 写二叉查找树练习

package main import ( "fmt" ) type item struct { key int } type tree struct { lchild, rchild *tree item item count int } func compare(x, y item) int { var ret int switch { case x.key > y.key: ret = 1 case x.key == y.key: ret = 0 case x.key < y.key: ret = -1 } return ret } func create(T *tree, x item) *tree { if T == nil { T = new(tree) T.item = x T...阅读全文

博文 2015-06-21 00:01:19 u014798316

Golang二分查找

注意: 二分查找只适用于有序序列 package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func main() { arr := []int{3, 4, 7, 9, 13, 45, 67, 89, 100, 180} // 排序前的数值 fmt.Println("数组中的数据: ") fmt.Println(arr) fmt.Print("输入查找的值: ") reader := bufio.NewReader(os.Stdin) str, _ := reader.ReadString('\n') str = strings.Replace(str, " ", "", -1) str = strings.Replace(...阅读全文

博文 2020-03-15 09:32:44 时光少年春

go package列表泛型包

# package slicelement Go library for finding element in slice type or operating set including union, interaction and difference. not only it supports the buildin types which includes `[]int/[]*int`, `[]float/[]*float`, `[]string/[]*string`, but also it supports `[]struct/[]*struct` . The latter is very important and convenient ## Installati...阅读全文

Golang二分查找(适用任意类型,只需实现接口)

package main import ( "fmt" ) type SortedList interface { Len() int Cmp(i int) int8 } type Person struct { age int } type SortedIntList []Person func (p Person) Cmp(cmp Person) int8 { if p.age == cmp.age { return 0 } else if p.age > cmp.age { return 1 } else { return -1 } } func (s SortedIntList) Cmp(i int) int8 { return s[i].Cmp(s[len(s)-1]) } fun...阅读全文

博文 2019-07-30 03:32:38 MrCloudPeak

golang实现二叉查找树

为简单起见,键值均为整型。 定义接口(tree.go): type Tree interface { Put(k, v int) //新增或修改 Get(k int) int //查询 Delete(k int) //删除 Size() int //树的大小 Min() int //最小键 DeleteMin() //删除最小键 } 二叉查找树(binary_tree.go): //二叉查找树 type BinaryTree struct { root *node n int } //创建节点 func newNode(k, v int) *node { return &node{k: k, v: v, sz: 1} } //创建二叉查找树 func NewBinaryTree() *Bin...阅读全文

博文 2017-09-03 07:05:00 imroc

元素查找包slicelement

一个 slicelement 包,用于查找一个元素,是否在列表中存在。支持 int、string、float 内置类型,也支持 struct 类型。比如:查找一个指定字段的元素值,是否在 []struct 中存在 我们经常遇到 append(data, element) 往列表中添加元素,如果 data 不存在该元素,则添加。还有一种情况也经常遇到的是,一个 []struct 数据复杂类型,判断 struct 中某个字段值是否存在,不存在则添加。 该 slicelement 包,支持这种查找,方便和节约了大家的宝贵时间 形如:  slicelement.Contains([]string{"Joe", "David", "Bruce Lee&#...阅读全文

Golang实现二叉搜索树

现在只实现了很简单的增加、查找、遍历。后面有空会完善。 package main import "fmt" type BNode struct { data int left *BNode right *BNode } type Tree struct { FirstNode *BNode } func (t *Tree) PrintData (node *BNode) { if node.left != nil { t.PrintData(node.left) } fmt.Println(node.data) if node.right != nil { t.PrintData(node.right) } return } // 返回node func (t *Tree) Search(d...阅读全文

博文 2018-04-21 12:33:04 丶老邪

golang 多线程查找文件内容

1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 ) 10 11 var matchCount = 0 12 var ch = make(chan int, 512) 13 14 func findFile(path string, text string) { 15 var pathArray [100]string 16 var count = 0 17 filepath.Walk(path, func(path string, info os.FileInfo, err error) error { 18 if err != nil { 19 } 20 //f...阅读全文

博文 2016-11-08 03:00:05 modprobe

数据结构与算法:二分查找

二分查找是搜索算法中的一种,用来搜索有序数组 二分查找: 是一种简单算法,其输入是一个有序的元素列表(必须有序的原因稍后解释)。如果要查找的元素包含在列表中,二分查找返回其位置;否则返回null。 Javascript ES6实现 /** * 函数binarySearch接受一个有序数组和一个元素。 如果指定的元素包含在数组中, 这个 函数将返回其位置。 你将跟踪要在其中查找的数组部分—— 开始时为整个数组。 */ const binarySearch = (list, item) => { // 数组要查找的范围 // low、high用于跟踪要在其中查找的列表部分 let low = 0 let high = list.length - 1 while(low <= high) { //...阅读全文

Golang LeetCode - 1. Two Sum 两数之和

Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. 思路分析 参数:一个数字数组nums,...阅读全文

博文 2020-03-31 23:32:59 Avery_up

golang regexp记录

FindAllSubmatch与FindSubmatch区别 // 匹配一个非元音字母,一个元音字母,一个非元音字母 someRegex, _ := regexp.Compile(`[^aouiye]([aouiye])([^aouiye])?`) m1 := someRegex.FindAllStringSubmatch("somestri", -1) m2 := someRegex.FindStringSubmatch("somestri") fmt.Println(m1) fmt.Println(m2) //result: [[som o m] [ri i ]] [som o m] re2, _ := regexp.Compile("am(.*)lang(.*)") //查找Subma...阅读全文

博文 2018-07-02 21:35:46 暮色伊人

Golang差劲的查找prime算法

pips/pip_prime.go package pips type PrimePip struct { } func NewPrimePip() *PrimePip { primePip := &PrimePip{} return primePip } func (primePip *PrimePip) RepeatFn( done <-chan interface{}, fn func() interface{}, ) <-chan interface{} { valueStream := make(chan interface{}) go func() { defer close(valueStream) for { select { case <-done: return case...阅读全文

博文 2019-06-17 23:32:41 FredricZhu

Nil Channels 的作用

思考来源于一次忘记初始化channel,造成程序卡死。 查找资料发现操作nil channels会永远阻塞,这个特性看起来似乎没什么用,甚至可能造成BUG,但Golang为何要保留这个特性? Google之:关键字 golang nil channel 相关讨论还挺多 甚至还有新鲜的关于Go 2的提议:proposal: language: Go 2: panic on send/receive on nil channel 以及Nil Chan的用法: nil-channels-always-block why-are-there-nil-channels-in-go 目前来说,nil chan只会用在select块中,用来关闭分支...阅读全文

博文 2020-02-13 09:32:46 bysir

go自定义一个定时器

### 定时器 + [查找更多案例](http://www.5lmh.com/ "查找更多案例") + Timer:时间到了,执行只执行1次 ```go package main import ( "fmt" "time" ) func main() { // 1.timer基本使用 //timer1 := time.NewTimer(2 * time.Second) //t1 := time.Now() //fmt.Printf("t1:%v\n", t1) //t2 := <-timer1.C //fmt.Printf("t2:%v\n", t2) // 2.验证timer只能响应1次 //timer2 := time.NewTimer(time.Second) //for { // <...阅读全文

博文 2019-11-27 16:44:33 lu569368

用 Go 语言实现一个 telegram 的 bot - 成功复读

telegram bot(复读机) 尝试 用 Go 语言做了一个 telegram 的 bot . 用来简单实现对话(复读)。 获得 telegram bot token 和 BotFather 交谈即可中途需要设置一下名字和查找路径 go get 首先获取 api 包 go get -u github.com/go-telegram-bot-api/telegram-bot-api code package main import ( "log" "os" "github.com/go-telegram-bot-api/telegram-bot-api" ) func main() { bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_A...阅读全文

博文 2019-07-26 19:02:37 yhyddr

leetcode_33

Golang: 思路:这大概是我提交错误最多的一次。思路很简单,先二分找到变化的点,知道变化的点后,查找可以去到数组的不同区域去二分。时间复杂度是logn没有问题。思路很正确没有问题。至于为什么错这么多,状态不好,没啥耐心,强行写题,炸了。顺便一提,这道题我的代码极烂,烂到我都不想承认这是我写的!!!后面会不会改要看心情。 代码如下: func search(nums []int, target int) int { if len(nums)==0 { return -1 } if len(nums)==1{ if target==nums[0]{ return 0 }else { return -1 } } temp:=pointChanged(nums) if temp==0 { tem...阅读全文

博文 2020-02-04 01:32:40 淳属虚构

leetcode-hot-(1/100)

1/100-两数之和 问题描述 link-to-leetcode 解法一:暴力搜索 暴力搜索是最简单的方法(笔者觉得从暴力搜索不断优化的过程是一个有趣的过程)时间复杂度:O(N^2)空间复杂度:O(1) func twoSum(nums []int, target int) []int { // loop: // 选择第一个数字 // 遍历后面的数字: // if 两个数字的和==target,then 输出结果 // 继续选择下一个数字 for i := 0; i < len(nums); i++ { for j := i + 1; j < len(nums); j++ { if nums[i] + nums[j] == target { return []int{i, j} } } } ...阅读全文

博文 2020-04-29 10:33:09 zhangshaos