go

Find index of a substring in a string, with start index specified

▼魔方 西西 提交于 2021-02-16 14:16:12
问题 I know there is strings.Index and strings.LastIndex , but they just find the first and last. Is there any function I can use, where I can specify the start index? Something like the last line in my example. Example: s := "go gopher, go" fmt.Println(strings.Index(s, "go")) // Position 0 fmt.Println(strings.LastIndex(s, "go")) // Postion 11 fmt.Println(strings.Index(s, "go", 1)) // Position 3 - Start looking for "go" begining at index 1 回答1: It's an annoying oversight, you have to create your

VS Code常用快捷键大全

谁都会走 提交于 2021-02-16 10:56:55
常用 General 按 Press 功能 Function Ctrl + Shift + P,F1 显示命令面板 Show Command Palette Ctrl + P 快速打开 Quick Open Ctrl + Shift + N 新窗口/实例 New window/instance Ctrl + Shift + W 关闭窗口/实例 Close window/instance 基础编辑 Basic editing 按 Press 功能 Function Ctrl+X 剪切行(空选定) Cut line (empty selection) Ctrl+C 复制行(空选定)Copy line (empty selection) Alt+ ↑ / ↓ 向上/向下移动行 Move line up/down Shift+Alt + ↓ / ↑ 向上/向下复制行 Copy line up/down Ctrl+Shift+K 删除行 Delete line Ctrl+Enter 在下面插入行 Insert line below Ctrl+Shift+Enter 在上面插入行 Insert line above Ctrl+Shift+\ 跳到匹配的括号 Jump to matching bracket Ctrl+] / [ 缩进/缩进行 Indent/outdent line Home

package code.google.com/p/go.example/hello: exec: “hg”: executable file not found in %PATH%. How to get remote golang packages?

我是研究僧i 提交于 2021-02-16 06:14:13
问题 I do as written an the Golang tutorial http://golang.org/doc/code.html#remote My env settings: C:\sbox\go\example>set go GOPATH=C:\sbox\go\example GOROOT=C:\Go The example/ folder has only src/ folder: C:\sbox\go\example\ | --src\ Now I call go get as described and get an error: C:\sbox\go\example>go get code.google.com/p/go.example/hello # cd .; hg clone -U https://code.google.com/p/go.example C:\sbox\go\example\src\code.google.com\p\go.example package code.google.com/p/go.example/hello:

package code.google.com/p/go.example/hello: exec: “hg”: executable file not found in %PATH%. How to get remote golang packages?

前提是你 提交于 2021-02-16 06:13:39
问题 I do as written an the Golang tutorial http://golang.org/doc/code.html#remote My env settings: C:\sbox\go\example>set go GOPATH=C:\sbox\go\example GOROOT=C:\Go The example/ folder has only src/ folder: C:\sbox\go\example\ | --src\ Now I call go get as described and get an error: C:\sbox\go\example>go get code.google.com/p/go.example/hello # cd .; hg clone -U https://code.google.com/p/go.example C:\sbox\go\example\src\code.google.com\p\go.example package code.google.com/p/go.example/hello:

Splitting a string at Space, except inside quotation marks

瘦欲@ 提交于 2021-02-16 04:34:38
问题 I was wondering if there is any way I could easily split a string at spaces, except when the space is inside quotation marks? For example, changing Foo bar random "letters lol" stuff into Foo , bar , random , "letters lol" , stuff 回答1: Think about it. You have a string in comma separated values (CSV) file format, RFC4180, except that your separator, outside quote pairs, is a space (instead of a comma). For example, package main import ( "encoding/csv" "fmt" "strings" ) func main() { s := `Foo

Splitting a string at Space, except inside quotation marks

送分小仙女□ 提交于 2021-02-16 04:32:18
问题 I was wondering if there is any way I could easily split a string at spaces, except when the space is inside quotation marks? For example, changing Foo bar random "letters lol" stuff into Foo , bar , random , "letters lol" , stuff 回答1: Think about it. You have a string in comma separated values (CSV) file format, RFC4180, except that your separator, outside quote pairs, is a space (instead of a comma). For example, package main import ( "encoding/csv" "fmt" "strings" ) func main() { s := `Foo

手撸golang 基本数据结构与算法 链表

℡╲_俬逩灬. 提交于 2021-02-15 19:54:38
手撸golang 基本数据结构与算法 链表 缘起 最近阅读<<我的第一本算法书>>(【日】石田保辉;宫崎修一) 本系列笔记拟采用golang练习之 链表 链表是数据结构之一,其中的数据呈线性排列。 每个数据节点都有1个“指针”,它指向下一个数据的内存地址。 访问数据时,我们需要从链表头部开始查找(线性查找), 如果目标数据在链表最后的话,需要的时间就是O(n)。 另外,添加数据只需要更改两个指针的指向,所以耗费的时间与n无关。 如果已经到达了添加数据的位置,那么添加操作只需花费O(1)的时间。 删除数据同样也只需O(1)的时间。 摘自 <<我的第一本算法书>>(【日】石田保辉;宫崎修一) 目标 实现一个链表, 提供与数组类似的线性访问接口 设计 ILinkedList: 链表接口 IListIterator: 链表迭代器接口 tLinkedList: 链表, 实现ILinkedList接口 tListIterator: 链表迭代器, 实现IListIterator接口 单元测试 linked_list_test.go package data_structure import ( "fmt" "learning/gooop/data_structure/linked_list" "strings" "testing" ) func Test_LinkedList(t

How to perform addToSet using Go official driver?

别来无恙 提交于 2021-02-15 07:09:15
问题 I need to do addToSet operation using official Go MongoDB driver. In MongoDB we have some docs: { _id: 2, item: "cable", tags: [ "electronics", "supplies" ] } Then to perform addToSet : db.inventory.update( { _id: 2 }, { $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } } ) Result: { _id: 2, item: "cable", tags: [ "electronics", "supplies", "camera", "accessories" ] } 回答1: $addToSet is an update operation, if you want to update a single document, you may use the

How to perform addToSet using Go official driver?

守給你的承諾、 提交于 2021-02-15 07:08:34
问题 I need to do addToSet operation using official Go MongoDB driver. In MongoDB we have some docs: { _id: 2, item: "cable", tags: [ "electronics", "supplies" ] } Then to perform addToSet : db.inventory.update( { _id: 2 }, { $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } } ) Result: { _id: 2, item: "cable", tags: [ "electronics", "supplies", "camera", "accessories" ] } 回答1: $addToSet is an update operation, if you want to update a single document, you may use the

How to json unmarshalling with custom attribute type in Go

一个人想着一个人 提交于 2021-02-15 07:06:42
问题 In my project, I've defined structures so that get data from JSON. I tried to use json.Unmarshal() function. But it did not work for custom type attribute. There was a structure like this: type TestModel struct { ID NullInt `json:"id"` Name string `json:"name"` } In there, NullInt type was defined with implementations of MarshalJSON() and UnmarshalJSON() functions: // NullInt ... type NullInt struct { Int int Valid bool } // MarshalJSON ... func (ni NullInt) MarshalJSON() ([]byte, error) { if