我有这个目录结构,我正在使用Gorilla mux:
目录结构
twitter
layout
stylesheets
log.css
log.html
twitter.go
Run Code Online (Sandbox Code Playgroud)
按照这里的建议:http://www.shakedos.com/2014/Feb/08/serving-static-files-with-go.html我这样做了:
var router = mux.NewRouter()
func ServeStatic(router *mux.Router, staticDirectory string) {
staticPaths := map[string]string{
"styles": staticDirectory + "stylesheets",
}
for pathName, pathValue := range staticPaths {
pathPrefix := "/" + pathName + "/"
router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
http.FileServer(http.Dir(pathValue))))
}
}
var staticDirectory = "/layout/"
func main() {
(//other code)
ServeStatic(router, staticDirectory)
}
Run Code Online (Sandbox Code Playgroud)
我还是无法链接CSS文件.我究竟做错了什么?
我正在尝试使用包含地图的数组创建地图.我的代码:
走:
func main() {
m := map[string][]map[string]string{
"photos": [{"a":"1"}, {"b": "2"}],
"pictures": [{"a":"1"}, {"b": "2"}]
}
fmt.Println(m)
}
Run Code Online (Sandbox Code Playgroud)
这有什么问题?这可能吗?
当我运行Go代码时,我不断收到此错误,该代码对我的本地postgres数据库进行查询.
错误:
panic serving [::1]:56708: runtime error: invalid memory address or nil pointer dereference
goroutine 23 [running]:
net/http.func·011()
/usr/local/go/src/pkg/net/http/server.go:1100 +0xb7
runtime.panic(0x2ef0a0, 0x4d8ee4)
/usr/local/go/src/pkg/runtime/panic.c:248 +0x18d
database/sql.(*DB).conn(0x0, 0x277a1, 0x0, 0x0)
/usr/local/go/src/pkg/database/sql/sql.go:625 +0x751
database/sql.(*DB).Ping(0x0, 0x0, 0x0)
/usr/local/go/src/pkg/database/sql/sql.go:452 +0x39
main.firstHandler(0x58e9a8, 0xc208052320, 0xc2080284e0)
/Users/Tommy/Documents/gocode/server/server.go:122 +0x35
net/http.HandlerFunc.ServeHTTP(0x3c6be8, 0x58e9a8, 0xc208052320, 0xc2080284e0)
/usr/local/go/src/pkg/net/http/server.go:1235 +0x40
github.com/gorilla/mux.(*Router).ServeHTTP(0xc2080186e0, 0x58e9a8, 0xc208052320, 0xc2080284e0)
/Users/Audrey/gocode/src/github.com/gorilla/mux/mux.go:98 +0x292
net/http.(*ServeMux).ServeHTTP(0xc208022660, 0x58e9a8, 0xc208052320, 0xc2080284e0)
/usr/local/go/src/pkg/net/http/server.go:1511 +0x1a3
net/http.serverHandler.ServeHTTP(0xc208004660, 0x58e9a8, 0xc208052320, 0xc2080284e0)
/usr/local/go/src/pkg/net/http/server.go:1673 +0x19f
net/http.(*conn).serve(0xc208050500)
/usr/local/go/src/pkg/net/http/server.go:1174 +0xa7e
created by net/http.(*Server).Serve
/usr/local/go/src/pkg/net/http/server.go:1721 +0x313
Run Code Online (Sandbox Code Playgroud)
走:
func firstHandler(w http.ResponseWriter, r *http.Request) {
err …Run Code Online (Sandbox Code Playgroud) 是否可以在地图的键中包含OR运算符?例如.
走:
var a = map[string]int{
"A1"|| "B1": 1,
"A2": 2,
}
Run Code Online (Sandbox Code Playgroud)
错误:
invalid operation: "A1" || "B1" (operator || not defined on string)
Run Code Online (Sandbox Code Playgroud) 我知道你可以指定命令行标志并对它们运行你的二进制文件,如下所示:
./binary -output=html -type=doc
Run Code Online (Sandbox Code Playgroud)
但是,我正在研究这个Go包的实现:https://github.com/jteeuwen/go-bindata
我想知道作者如何能够让用户运行这样的命令
go-bindata /data
Run Code Online (Sandbox Code Playgroud)
代替
./go-bindata -target=/data
Run Code Online (Sandbox Code Playgroud)
如果我错过任何东西,请感谢一些帮助!
我正在尝试访问命令行参数.如果论证存在,做一些事情,如果没有,什么都不做.我有这个代码:
fn main() {
if let Some(a) = std::env::args().nth(2) {
let b = a;
}
println!("{}", a);
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法访问此范围之外的var a或b.我该如何解决这个问题?
或者,有没有更好的方法来接近我正在尝试做的事情?