go

Basic API in golang antipattern?

孤者浪人 提交于 2021-01-29 22:34:13
问题 Correct me if I'm wrong, but for my understanding of an API is that it is something that allows me to modify and request data through an interface, which is what I want to do in Go. For example I have a user interface: interface IUser { GetId() int GetName() string GetSignupDate() time GetPermissions() []IPermission Delete() } This already looks to me like active record and if I want to create a new user with a new id I would have to use new since Go doesn't support static functions as far as

Why must I copy string before dereferencing?

风格不统一 提交于 2021-01-29 22:22:42
问题 To convert a map whose values are strings into one whose values are points to string, I need to copy the string first. If I do not, all the values are the same, potentially wrong value. Why is this? I am not taking the address of a string literal here. func mapConvert (m map[string]string) map[string]*string { ret := make(map[string]*string) for k, v := range m { v2 := v[:] ret[k] = &v2 // With the following instead of the last 2 lines, // the returned map have the same, sometimes wrong value

mongodb can't do transaction in Go and always got Cannot create namespace in multi-document transaction

安稳与你 提交于 2021-01-29 20:24:47
问题 I am trying to create a function that InsertOne data by wrap transaction, and I already to replica set in mongodb also, I tried in local and and MongoDB atlas the error were same, here is the code: const MONGODB_URI = "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs" ctx := context.Background() client, err := mongo.Connect(ctx, options.Client().ApplyURI(MONGODB_URI)) if err != nil { panic(err) } db := client.Database("production") defer db.Client().Disconnect(ctx) col

Fileserver directory for dynamic route

心不动则不痛 提交于 2021-01-29 20:06:37
问题 My scenario Compiled angular projects are saved like . ├── branch1 │ ├── commitC │ │ ├── app1 │ │ │ ├── index.html │ │ │ └── stylesheet.css │ └── commitD │ ├── app1 │ │ ├── index.html │ │ └── stylesheet.css │ └── app2 │ ├── index.html │ └── stylesheet.css ├── branch2 │ ├── commitE │ ├── app1 │ │ ├── index.html │ │ └── stylesheet.css │ └── app2 │ ├── index.html │ └── stylesheet.css └── master ├── commitA │ ├── app1 │ │ ├── index.html │ │ └── stylesheet.css └── commitB ├── app1 ├── index.html └

SQLX “missing destination name” when using table name in the struct tag

本小妞迷上赌 提交于 2021-01-29 19:35:27
问题 The issue is that when I use struct tags with an object, they do not work properly. I've worked on projects before that have done the same thing but have had no issue, but I can't figure out why. Example: this does not work: type Category struct { ID int `json:"id" db:"category.id"` Name string `json:"name" db:"category.name"` Description string `json:"description" db:"category.description"` } error received: missing destination name id in *[]Category this works fine: type Category struct {

TCP fixed size message framing method in Golang

这一生的挚爱 提交于 2021-01-29 19:25:12
问题 I'm not able to understand how message framing using fixed size prefix length header works. It's said that a fixed size byte array is going to contain the length for message to be sent. But how would you define a fixed size byte array, specifically in Golang. Say this is my message: Hello Its length is 5. So if I want to send this through a tcp stream, to make sure I receive all the message on the other end I'd have to tell it how many bytes it should read. A simple header would be length

Go: efficiently handling fragmented data received via TCP

空扰寡人 提交于 2021-01-29 17:12:40
问题 I am writing a small tcp-server that is only suppose to read data, parse the receiving data (dynamic length frames) and handling these frames. Consider the following code: func ClientHandler(conn net.Conn) { buf := make([]byte, 4096) n, err := conn.Read(buf) if err != nil { fmt.Println("Error reading:", err.Error()) return } fmt.Println("received ", n, " bytes of data =", string(buf)) handleData(buf) This is pretty much the essence of my code as is. I read X bytes into a empty buffer and then

mongodb can't do transaction in Go and always got Cannot create namespace in multi-document transaction

三世轮回 提交于 2021-01-29 17:11:42
问题 I am trying to create a function that InsertOne data by wrap transaction, and I already to replica set in mongodb also, I tried in local and and MongoDB atlas the error were same, here is the code: const MONGODB_URI = "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs" ctx := context.Background() client, err := mongo.Connect(ctx, options.Client().ApplyURI(MONGODB_URI)) if err != nil { panic(err) } db := client.Database("production") defer db.Client().Disconnect(ctx) col

Cannot connect to graphql playground when using go-chi router

Deadly 提交于 2021-01-29 16:19:42
问题 I'm use gqlgen to create a go graphql server. In the tutorial, the default setup for localhost:8080 works fine. server.go ... func main() { srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}})) http.Handle("/", playground.Handler("GraphQL playground", "/query")) http.Handle("/query", srv) log.Fatal(http.ListenAndServe(":8080", nil)) } ... However, if i switch to the Chi package router, I get a 400 error: server.go ... func main() {

How to append to a 2d slice

流过昼夜 提交于 2021-01-29 16:14:01
问题 I have data that is created rows by rows, 6 columns, I don't know the final number of rows in advance. Currently i'm creating a 2D slice of 200x6 with all zeros and then i replace these zeros gradually with my data, row by row. The data comes from another dataframe df It works but i don't like to end up with the last rows of my slice full of zeros. I see 2 solutions: - I delete all the last rows with only zeros when I'm done - I create an empty slice and append my data progressively to it I