golang

Static file server in Golang using gorilla/mux

匿名 (未验证) 提交于 2019-12-03 01:39:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have made a app where I need to serve the same files to multiple routes because the front end is a React app. I have been using gorilla mux for the router. The file structure is as follows: main . go build / | index . html | service - worker . js static / | js / | main . js | css / | main . css The files are refereed to assuming they are at the root of the file directory. So in the html file they are requested like '/static/js/main.js'. In main my routes are defined as follows: r . PathPrefix ( "/student" ). Handler ( http .

golang exec.Command read std input

匿名 (未验证) 提交于 2019-12-03 01:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a go program that should invoke a ruby script. I have a runCommand function: func runCommand(cmdName string, arg ...string) { cmd := exec.Command(cmdName, arg...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Stdin = os.Stdin err = cmd.Run() if err != nil { fmt.Printf("Failed to start Ruby. %s\n", err.Error()) os.Exit(1) } } I invoke it like this: runCommand("ruby", "-e", "require 'foo'") It works for most cases, except if there is a gets or any similar operation in the child process that needs to pause for an input. I have tried

How to use golang lego let's encrypt client behind nginx?

匿名 (未验证) 提交于 2019-12-03 01:36:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'd like to setup Let's Encrypt certificate to live server with nginx with lego client written in Go https://github.com/xenolf/lego/ What I'll to do with nginx config to get certificate? 回答1: You need to add in :80 and :443 virtual servers following location: # http and https nginx servers location /.well-known/acme-challenge/ { proxy_set_header Host $host; proxy_pass http://127.0.0.1:4000$request_uri; } And run lego binary: ./lego.amd64 --http 127.0.0.1:4000 --email="your@address.tld" --domains domain.tld --domains some.domain.tld run Your

Golang: convert slices into map

匿名 (未验证) 提交于 2019-12-03 01:33:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there an easy/simple means of converting a slice into a map in Golang? Like converting an array into hash in perl is easy to do with simple assignment like %hash = @array this above will convert all the elements in the array into a hash, with keys being even-numbered index elements while the values will be odd-numbered index elements of the array. In my Go code, I have slices of string and would like to convert it into a map. I am wondering if there is a Go's library code to do this. func main() { var elements []string var elementMap map

Golang how do I batch sql statements with package database.sql

匿名 (未验证) 提交于 2019-12-03 01:25:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How do I batch sql statements with Golang's database.sql package? In Java I would do it like this : // Create a prepared statement String sql = "INSERT INTO my_table VALUES(?)"; PreparedStatement pstmt = connection.prepareStatement(sql); // Insert 10 rows of data for (int i=0; i How would I achieve the same in Golang? 回答1: Notabene: Although this answer is marked as accepted, it is wrong (see discussion in comments below). The only reason this answer still exists is because I'm unable to delete it since it was marked as accepted. I don't

Golang JSON omitempty With time.Time Field

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Trying to json Marshal a struct that contains 2 time fields. But I only want the field to come through if it has a time value. So I'm using json:",omitempty" but it's not working. What can I set the Date value to so json.Marshal will treat it like an empty (zero) value and not include it in the json string? Playground: http://play.golang.org/p/QJwh7yBJlo Actual Outcome: {"Timestamp":"2015-09-18T00:00:00Z","Date":"0001-01-01T00:00:00Z"} Desired Outcome: {"Timestamp":"2015-09-18T00:00:00Z"} Code: package main import ( "encoding/json" "fmt"

Compare error message in golang

匿名 (未验证) 提交于 2019-12-03 01:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Let's say I create a new error in golang like so err := errors . New ( "SOME_COMMON_ERROR_CODE" ) In java, I'm used to being able to get Exception with GetMessage() messages. How would I compare that error if returned? if some_err := some_package . DoSomething (); some_err != nil { if some_err . GetMessage () == "SOME_COMMON_ERROR_CODE" { // handle it however. } } How is this done in golang? 回答1: Declare a package level variable with the error: var errExample = errors . New ( "this is an example" ) Use this value when returning

golang gdb - print variables

匿名 (未验证) 提交于 2019-12-03 01:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a problem with gdb not printing variables correctly. Simple program is build in following way: chmurson-osx:helloworld chmurson$ go build -gcflags '-N' start.go And then gdb executed: chmurson-osx:helloworld chmurson$ gdb start -d $GOROOT GNU gdb (GDB) 7.8 Copyright (C) 2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show

golang “undefined” function declared in another file?

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to write a basic go program that calls a function on a different file, but a part of the same package. However, it returns: undefined: NewEmployee Here is the source code: main.go : package main func main() { emp := NewEmployee() } employee.go : package main type Employee struct { name string age int } func NewEmployee() *Employee { p := &Employee{} return p } func PrintEmployee (p *Employee) { return "Hello world!" } Thanks in advance 回答1: Please read "How to Write Go Code" . Don't use /src in your GOPATH . Packages are located

Golang multiple files in main package

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I currently have a single file in my main package called main.go . How do I split the contents of main.go into multiple files without creating a separate package because the code isn't reusable. I want a directory structure like this: $ ls foo main.go bar.go bar.go package main import "fmt" func Bar() { fmt.Println("Bar") } Then in main.go package main func main() { Bar() } 回答1: The code above actually works. The problem was I needed to run go run *.go instead of go run main.go 回答2: As mentioned in " How to compile Go program consisting of