go

Specifying the JSON name for protobuf extension

陌路散爱 提交于 2021-02-07 12:46:36
问题 I've added an extending message to a message and need to marshal it as a json. However the field name for the extension message is [message.extension_message_name] . I would prefer it to be named just extension_message_name , without the braces and prefix, since this extension message exists elsewhere in our API and and having this weird name adds confusion. As far as I can tell the bit of code responsible is in protobuf/jsonpb, where the JSONName is set with fmt.Sprintf("[%s]", desc.Name and

Reading files with a BOM in Go

五迷三道 提交于 2021-02-07 12:32:51
问题 I need to read Unicode files that may or may not contain a byte-order mark. I could of course check the first few bytes of the file myself, and discard a BOM if I find one. But before I do, is there any standard way of doing this, either in the core libraries or a third party? 回答1: No standard way, IIRC (and the standard library would really be a wrong layer to implement such a check in) so here are two examples of how you could deal with it yourself. One is to use a buffered reader above

Reading files with a BOM in Go

霸气de小男生 提交于 2021-02-07 12:32:09
问题 I need to read Unicode files that may or may not contain a byte-order mark. I could of course check the first few bytes of the file myself, and discard a BOM if I find one. But before I do, is there any standard way of doing this, either in the core libraries or a third party? 回答1: No standard way, IIRC (and the standard library would really be a wrong layer to implement such a check in) so here are two examples of how you could deal with it yourself. One is to use a buffered reader above

kuiper批量创建规则的例子

删除回忆录丶 提交于 2021-02-07 12:17:50
引言 前面提到了 测试kuiper创建规则上限数 遇到的问题,这里给大家分享一下如何批量创建多条规则。 分析 kuiper官网的性能测试结果中没有详细说明8000条规则的具体场景。这里我是这么理解: 首先有8000个流,其中800个流对应一个SELECT temperature FROM sourceX WHERE > 20这样的规则;另外7200个流对应SELECT temperature FROM sourceY WHERE temperature <= 20 这样如果我发送的MQTT消息中的 temperature 为(20,100]间的随机数,整个7200/8000=90%的数据被过滤掉,只有800/8000=10%的规则被命中。 测试场景构建 创建两个流:demo1和demo2 #进入容器 docker exec -it kuiper /bin/bash #创建流demo1 bin/kuiper create stream demo1 '(temperature float, humidity bigint) WITH (FORMAT="JSON", DATASOURCE="demo1")' #创建流demo2 bin/kuiper create stream demo2 '(temperature float, humidity bigint) WITH (FORMAT

golang slice in mysql query with where in clause

人盡茶涼 提交于 2021-02-07 11:43:05
问题 I am trying to run query like below , but only first id value returns :- select * from `table` where table`.`id` in ('1', '2', '3', '4', '5', '6', '7', '9', '11', '13', '14', '15', '17') and `table`.`deleted_at` is null I have done following :- var aID = make([]string, 0) var in india // india is struct for rows.Next() { cook := rows.Scan(&in.ID) aID = append(aID, strconv.Itoa(in.ID)) } asID = strings.Join(aID, ",") anotherRow,err := db.Query("SELECT * from table2 where id in (?)", asID) if

Passing an optimization flag to a Go compiler?

梦想与她 提交于 2021-02-07 11:33:12
问题 To compile a Go program you type go build myprogram.go , can you pass an optimization flags along or the code is always compiled in the same way? I am talking about speed optimizations, code size optimizations or other optimizations. I know if you use gccgo you just pass -O2 or -O0 but my question is about an official Go compiler go . 回答1: Actually no explicit flags, this Go wiki page lists optimizations done by the Go compiler and there was a discussion around this topic in golang-nuts

Passing an optimization flag to a Go compiler?

我与影子孤独终老i 提交于 2021-02-07 11:33:03
问题 To compile a Go program you type go build myprogram.go , can you pass an optimization flags along or the code is always compiled in the same way? I am talking about speed optimizations, code size optimizations or other optimizations. I know if you use gccgo you just pass -O2 or -O0 but my question is about an official Go compiler go . 回答1: Actually no explicit flags, this Go wiki page lists optimizations done by the Go compiler and there was a discussion around this topic in golang-nuts

Does it make sense to add `go mod vendor` to a pre-commit hook?

拈花ヽ惹草 提交于 2021-02-07 10:59:35
问题 Setup: Our project is using golang 1.12.14 We are using go build -mod=vendor Issue: When new dependencies are added to go.mod the vendor folder isn't updated and people are committing code and forgetting to run go mod vendor to update the folder. My understanding is that since -mod=vendor specifies to use packages from the vendor folder, the go.mod file will have discrepancies from what we are actually using when building the project. Question: Should go mod vendor be added to a pre-commit

How to populate and embedded array with gorm?

南笙酒味 提交于 2021-02-07 10:56:17
问题 I have 2 structs with data like this: type User struct { Pics Pic[] } type Pic struct { Id int UserId int64 } Although everytime I insert an User, Each of the pics are inserted on their table everytime I find the users, pics are not populated: var users []User db.Limit(pagesize).Where("updated_at > ?", date).Find(&users) Am I doing something wrong? 回答1: Your models (the structs) don't really make sense because User have a Pic array indicates a 'one to many' user to pics relationship however

Go-idiomatic naming of slice types

╄→尐↘猪︶ㄣ 提交于 2021-02-07 10:46:20
问题 When I need methods on slices I have to declare a new type. But what should I name it? type SliceSomething []Something or type SomethingSlice []Something ? Since it's read as "slice of something" the first one seems better, but autocomplete would probably prefer the second one. 回答1: The CodeReview wiki page Variable names in Go should be short rather than long. This is especially true for local variables with limited scope. Prefer c to lineCount . Prefer i to sliceIndex . The basic rule: the