go

golang dispatch method call according to a map[string]somestruct

一曲冷凌霜 提交于 2021-01-29 07:51:51
问题 Assuming I have lots of functions or methods with receiver, each of which has different type of parameters. I want to use table-driven method to dispatch function or method call. So I'll construct a table like this: type command struct { name string handler func(parameter ...interface{}) // I don't know whether to use `...interface{}` is correct } table := map[string]command { ... } func (c command)foo(f1 int, f2 string) {} func (c command)bar(b1 bool, b2 int, b3 string) {} // methods and so

Comparison between empty interfaces in Golang

无人久伴 提交于 2021-01-29 07:33:23
问题 According to the specification: Interface values are comparable. Two interface values are equal if they have identical dynamic types and equal dynamic values or if both have value nil. var err error var reader io.Reader As far as understand, err and reader have different dynamic types ( error and io.Reader ) and therefore are not comparable. fmt.Println(err == reader) will cause a compile error: invalid operation: err == reader (mismatched types error and io.Reader) If it is true, why Println

How to read files outside main.go, and make it work on AWS lambda

我的未来我决定 提交于 2021-01-29 07:15:10
问题 On AWS Lambda, I want to use CSV file outside main.go Of course this works when I follow these steps. cd ~/go/src/lambda-go-sample GOOS=linux go build main.go zip main.zip main upload to Lambda and test => "Hello ƛ!" main.go package main import ( "github.com/aws/aws-lambda-go/lambda" ) func hello() (string, error) { return "Hello ƛ!", nil } func main() { lambda.Start(hello) } Now I add sample.csv on the same directory of main.go ,and add code to read it. lambda-go-sample |- main.go |- sample

aws sdk for go forbidden but aws cli works fine

回眸只為那壹抹淺笑 提交于 2021-01-29 06:13:52
问题 We are using aws cli and it works perfectly, we can create taskdefinitions, create services, etc. The problem comes when we want to use the sdk for go. We always get a forbidden. We have tried to use a new empty session, a session specifying the region, asuming roles... nothing works. Does anyone know how we could solve it? We have tried many things: - setting a new config when creating a new session: Config: aws.Config{ CredentialsChainVerboseErrors: aws.Bool(true), Credentials: credentials

In Cluster Config is unable to get pods when deployed in a non-default namespace

北慕城南 提交于 2021-01-29 06:11:53
问题 When I deploy my golang service to any namespace but the default namespace, the service is unable to retrieve pods on any namespace. The same service deployed on the default namespace works perfectly, using the golang client-go api. Is this a security issue? Thanks. 回答1: This issue is permission issue. Since you are using rest.InClusterConfig(config) to create client. That means it using pod's service account as credential. So check whether that service account has the permission to get pods

GO Mongodb时间处理

回眸只為那壹抹淺笑 提交于 2021-01-29 05:28:56
go在操作monogdb时间的时候如果使用time.Time时间格式,则前后端的时间显示是这样的:2021-01-28T06:38:43.622Z;这种格式很不友好(时区还不对),更多时候我们可能需要的是这种格式:2021-01-28 14:38:43;在网上查了一些资料,发现解决方案都不太好,有的是要手动做转换,有的是改变了mongodb的存储内容;我这里综合了一些资料给出以下解决方法(通过申明time.Time的扩展,重写json和bson序列化代码实现): 序列化处理代码: package common import ( "errors" "fmt" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" "time" ) type JsonTime time.Time const ( timeFormart = "2006-01-02 15:04:05" ) //实现json反序列化,从传递的字符串中解析成时间对象 func (t *JsonTime) UnmarshalJSON(data [

missing type in composite literal go AND missing key in map literal go

橙三吉。 提交于 2021-01-29 05:13:44
问题 Im trying to do Pagination with MongoDB I write this code: findOptions := options.Find() findOptions.SetLimit(20) findOptions.SetSort(bson.M{{"_id", 1}}) cursor, err34 := collection.Find(context.Background(), bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions) Now It keeps complaining: missing type in composite literal go AND missing key in map literal go It complains for this part: findOptions.SetSort(bson.M{{"_id", 1}}) and bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions) I'm

Wrapping allocated byte buffer in C as Go slice ([]byte) [duplicate]

℡╲_俬逩灬. 提交于 2021-01-29 05:04:06
问题 This question already has answers here : casting a cgo array into a slice (1 answer) Access C array of type const char * from Go (1 answer) Go slice backed by a C array [duplicate] (1 answer) What is the best (safest + most performant) way of getting a specific slice of bytes from an unsafe.Pointer (1 answer) What does (*[1 << 30]C.YourType) do exactly in CGo? (1 answer) Closed 8 months ago . I have a use-case where C code will call Go function (exported by building Go as shared library). All

Can I lock using specific values in Go?

*爱你&永不变心* 提交于 2021-01-29 04:44:28
问题 In answering another question I wrote a little struct using sync.Map to cache API requests. type PostManager struct { sync.Map } func (pc PostManager) Fetch(id int) Post { post, ok := pc.Load(id) if ok { fmt.Printf("Using cached post %v\n", id) return post.(Post) } fmt.Printf("Fetching post %v\n", id) post = pc.fetchPost(id) pc.Store(id, post) return post.(Post) } Unfortunately, if two goroutines both fetch the same uncached Post at the same time, both will make a request. var postManager

Decode Hex Timestamp, probably filetime

不问归期 提交于 2021-01-29 04:40:24
问题 im struggling with a timestamp i want to convert to human readable format, it is from OPCUA protocol and is supposed to be in hundreds of nanoseconds since 1. Jan 1601, so it should be filetime. However, trying various solutions posted here i am not able to convert it to the right time. Here are some examples, 1c67dc4ab30dd201 -> Sep 13, 2016 13:38:05.343106800 CET 15605a199070d201 -> Jan 17, 2017 08:05:35.012046900 CET 322b4f629970d201 -> Jan 17, 2017 09:12:02.882846600 CET As wireshark is