go

How to parse JSON arrays with two different data types into a struct in Golang

做~自己de王妃 提交于 2021-01-27 22:13:33
问题 I have JSON that looks like this: { "a": [ [ "12.58861425", 10.52046452 ], [ "12.58861426", 4.1073 ] ] "b": [ [ "12.58861425", 10.52046452 ], [ "12.58861426", 4.1073 ] ] "c": "true" "d": 1234 } I want to unmarshall this into a struct I created: type OrderBook struct { A [][2]float32 `json:"a"` B [][2]float32 `json:"b"` C string `json:"c"` D uint32 `json:"d"` } //Note this won't work because the JSON array contains a string and a float value pair rather than only floats. Normally I would then

Go viper .yaml values environment variables override

拟墨画扇 提交于 2021-01-27 22:08:55
问题 I'm trying to have application.yaml file in go application which contains ${RMQ_HOST} values which I want to override with environment variables. In application.yaml I've got: rmq: test: host: ${RMQ_HOST} port: ${RMQ_PORT} And in my loader I have: log.Println("Loading config...") viper.SetConfigName("application") viper.SetConfigType("yaml") viper.AddConfigPath(".") viper.AutomaticEnv() err := viper.ReadInConfig() The problem I have is ${RMQ_HOST} won't get replaced by the values I've set in

Golang MySQL Database Not Selected

Deadly 提交于 2021-01-27 21:22:08
问题 I'm using github.com/go-sql-driver/mysql package to connect to MySQL. It works well except when I select a database ( USE ), I can't run queries against it. package main import ( "database/sql" "fmt" "log" ) import _ "github.com/go-sql-driver/mysql" func main() { dsn := "root:@/" db, err := sql.Open("mysql", dsn) if err != nil { fmt.Println("Failed to prepare connection to database. DSN:", dsn) log.Fatal("Error:", err.Error()) } err = db.Ping() if err != nil { fmt.Println("Failed to establish

How to add padding, font size and colours to fyne UI app

烂漫一生 提交于 2021-01-27 21:11:19
问题 I have this sample app code below for a UI I'm implementing using the fyne toolkit, and cant figure out how to align the buttons to the left, makes top text bigger, and add colours. I've tried trying to create a custom theme to implement the UI features I need, but my understanding of the godoc for the fyne toolkit is lacking. Is there a doc someone can point me to to make this work? or provide me with some pointers, as the toolkit is poorly documented this is my sample app code package main

How to run mongo command with mongo-go-driver?

不打扰是莪最后的温柔 提交于 2021-01-27 19:11:04
问题 Hi there :) I'm working on a golang app linked to mongo DB (I use the official driver: mongo-go ) and here's my problem,I want to execute this function db.rmTickets.find().forEach(function(doc) { doc.created=new Date(doc.created) doc.updated=new Date(doc.updated) doc.deadline=new Date(doc.deadline) doc.dateEstimationDelivery=new Date(doc.dateEstimationDelivery) doc.dateTransmitDemand=new Date(doc.dateTransmitDemand) doc.dateTransmitQuotation=new Date(doc.dateTransmitQuotation) doc

How to enable race detector for Golang in Visual Studio Code?

女生的网名这么多〃 提交于 2021-01-27 18:08:58
问题 I searched in many web pages to find what phrase exact should I place in settings.json in VS Code Golang extension (released by Microsoft) to add a build flag (in my case, race detector)? I added: "go.buildFlags": ["-race"], in extension's settings.json but still when debugging, in a function that definitely generates a race condition, the debugging console does not denote something like: Found 1 data race(s) which the go compiler usually indicates when executing the same file via go run

Why does initializing just one variable in a definition fail, in golang

强颜欢笑 提交于 2021-01-27 17:23:30
问题 In calling a library function with the following signature: func New() (*sql.DB, Sqlmock, error) like this: suite.db, suite.mock, err := sqlmock.New() // inside a suite method I get error expected identifier on left side of := However, when I change to this var err error suite.db, suite.mock, err = sqlmock.New() the error disappears! Why does declaring k < n variables in a := assignment fail?! 回答1: := is not assignment, it is a short variable declaration. Assignment uses e.g. the simple =

GoDoc add newline character

删除回忆录丶 提交于 2021-01-27 16:01:27
问题 I know that Golang supports documentation of functions via single-line comments starting with the name of the function (spelled "func"). However, there's a nauseating side effect: having multiple single line comments will not produce a GoDoc with newline characters separating each line of text Here's a pic to illustrate: Here's the func, and its documentation: //GetFunctionName gets function name // Parameters: // - `i` : Function // **NOTE** this func fails if `i` is a variable set to a func

Go Protobuf declarations and optional Fields in Go Struct (string pointers)

不打扰是莪最后的温柔 提交于 2021-01-27 14:09:17
问题 I am running into a bit of a problem with Protoc and my existing struct that contains nullable string fields. The struct I am trying to serialize for transmission contains a bunch of fields that are nullable in json (so we can distinguish between null , "" and a set value). type Message struct { Path *string `json:"path"` } So if a user sends a empty json string {} the Path will be nil and not "" , whereas {"path":""} is also valid and a different case from {"path": null} . The proto3

Failure to connect to proxy “Certificate signed by unknown authority”

Deadly 提交于 2021-01-27 13:42:34
问题 I'm attempting to connect to a CloudSQL instance via a cloudsql-proxy container on my Kubernetes deployment. I have the cloudsql credentials mounted and the value of GOOGLE_APPLICATION_CREDENTIALS set. However, I'm still receiving the following error in my logs: 2018/10/08 20:07:28 Failed to connect to database: Post https://www.googleapis.com/sql/v1beta4/projects/[projectID]/instances/[appName]/createEphemeral?alt=json&prettyPrint=false: oauth2: cannot fetch token: Post https://oauth2