go

Pass full slice range as parameter

爱⌒轻易说出口 提交于 2021-02-05 07:40:32
问题 Considering the code below, I have seen some code using this format v[:] for pass full slice (not part of it) as parameter. Is there any difference between v[:] and v ? Or it is just a developer preference? The test I did below show me no difference. Am I missing something? package main import ( "fmt" ) func main() { v := []byte {1, 2, 3} printSliceInfo(v) printSliceInfo(v[:]) } func printSliceInfo(s []byte) { fmt.Printf("Len: %v - Cap: %v - %v\n", len(s), cap(s), s) } 回答1: When v is a slice,

hmac hash mismatch in PHP and Go

痞子三分冷 提交于 2021-02-05 07:17:35
问题 I am trying to connect to an API that uses an outdated hmac hash authentication mechanism for the API's. For an instance: $signature = hash_hmac('sha256', $string_to_sign, $api_sec); vs the one generated in Go: h := hmac.New(sha256.New, []byte(authSecret)) h.Write([]byte(stringToSign)) signature := hex.EncodeToString(h.Sum(nil)) When I use the same stringToSign($string_to_sign) and same authSecret($api_sec) the signature generated with Go results as an invalid signature for the API. But if I

hmac hash mismatch in PHP and Go

巧了我就是萌 提交于 2021-02-05 07:17:25
问题 I am trying to connect to an API that uses an outdated hmac hash authentication mechanism for the API's. For an instance: $signature = hash_hmac('sha256', $string_to_sign, $api_sec); vs the one generated in Go: h := hmac.New(sha256.New, []byte(authSecret)) h.Write([]byte(stringToSign)) signature := hex.EncodeToString(h.Sum(nil)) When I use the same stringToSign($string_to_sign) and same authSecret($api_sec) the signature generated with Go results as an invalid signature for the API. But if I

How to get a function's signature as string in go

﹥>﹥吖頭↗ 提交于 2021-02-05 07:14:45
问题 I'm implementing a go module that loads go plugins. I'm assuming a function with a certain name and a certain signature exists on the main package, and would like to have a nice error message in case it is not found or not matching the expected signature. Given a variable with a function type, how can one get the underlying signature of that function? The following only prints the type's name (e.g. main.ModuleInitFunc ) and not the full signature. package main import "fmt" type ModuleInitFunc

“go get” git error on all sources but golang.org

ぐ巨炮叔叔 提交于 2021-02-05 06:40:11
问题 go version go1.5.1 windows/amd64 git version 1.9.5.msysgit.1 I have been trying to get some Go libraries. They are downloaded fine, when accessed on golang.org but github.com or google.golang.org packages give an error. There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details 回答1: Delete the package from GOPATH and go get it again. Your package is modified and git is refusing to pull from upstream. 回答2: Have you

selecting 2D sub-slice of a 2D-slice using ranges in go

大憨熊 提交于 2021-02-05 06:17:05
问题 I'm getting a surprising result when selecting a 2D sub-slice of a slice. Consider the following 2D int array a := [][]int{ {0, 1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}, } To select the top left 3x3 2D slice using ranges I would use b := a[0:2][0:2] I would expect the result to be [[0 1 2] [1 2 3] [2 3 4]] however the second index range doesn't seem to have any effect, and returns the following instead: [[0 1 2 3] [1 2 3 4] [2 3 4 5]] What am I missing? Can you simply not select a

Capturing repeating groups in GO

你说的曾经没有我的故事 提交于 2021-02-05 06:11:10
问题 I'm trying to create a function that can parse strings which consist of an uppercase word followed by zero or more arguments which are encapsulated in double quotes. For example, each of the following lines: COPY "filename one" "filename two" REMOVE "filename" LIST "x" "y" "z" DISCONNECT The result should be a string (the command) followed by a string[] (the arguments inside the quotes). I created the following regular expression: re1, _ := regexp.Compile(`([A-Z]+)(?: "([^"]+)")*`) results :=

Capturing repeating groups in GO

◇◆丶佛笑我妖孽 提交于 2021-02-05 06:05:12
问题 I'm trying to create a function that can parse strings which consist of an uppercase word followed by zero or more arguments which are encapsulated in double quotes. For example, each of the following lines: COPY "filename one" "filename two" REMOVE "filename" LIST "x" "y" "z" DISCONNECT The result should be a string (the command) followed by a string[] (the arguments inside the quotes). I created the following regular expression: re1, _ := regexp.Compile(`([A-Z]+)(?: "([^"]+)")*`) results :=

Capturing repeating groups in GO

主宰稳场 提交于 2021-02-05 06:05:01
问题 I'm trying to create a function that can parse strings which consist of an uppercase word followed by zero or more arguments which are encapsulated in double quotes. For example, each of the following lines: COPY "filename one" "filename two" REMOVE "filename" LIST "x" "y" "z" DISCONNECT The result should be a string (the command) followed by a string[] (the arguments inside the quotes). I created the following regular expression: re1, _ := regexp.Compile(`([A-Z]+)(?: "([^"]+)")*`) results :=

Why is atomic.StoreUint32 preferred over a normal assignment in sync.Once?

▼魔方 西西 提交于 2021-02-05 04:59:40
问题 While reading the source codes of Go, I have a question about the code in src/sync/once.go: func (o *Once) Do(f func()) { // Note: Here is an incorrect implementation of Do: // // if atomic.CompareAndSwapUint32(&o.done, 0, 1) { // f() // } // // Do guarantees that when it returns, f has finished. // This implementation would not implement that guarantee: // given two simultaneous calls, the winner of the cas would // call f, and the second would return immediately, without // waiting for the