小编Phi*_*ide的帖子

如何将参数传递给我的处理程序

我试图将我的数据库对象传递给我的处理程序,而不是拥有一个全局对象.但我不知道这是否可行,我正在使用Gorilla Mux包,我可以看到它需要一个封闭作为第二个参数.

// https://github.com/gorilla/mux/blob/master/mux.go#L174
// HandleFunc registers a new route with a matcher for the URL path.
// See Route.Path() and Route.HandlerFunc().
func (r *Router) HandleFunc(path string, f func(http.ResponseWriter,
    *http.Request)) *Route {
    return r.NewRoute().Path(path).HandlerFunc(f)
}
Run Code Online (Sandbox Code Playgroud)

然后定义了我可以使用的参数,理想情况下我希望有这样的第三个参数.

// In my main
router.HandleFunc("/users/{id}", showUserHandler).Methods("GET")

func showUserHandler(w http.ResponseWriter, r *http.Request, db *gorm.DB) {
    fmt.Fprintf(w, "We should fetch the user with id %s", vars["id"])
}
Run Code Online (Sandbox Code Playgroud)

有解决方法吗?或者我需要一个全局数据库对象?我是Go的新手,所以请详细解释一个可能的答案.

go gorilla

32
推荐指数
1
解决办法
2万
查看次数

如何使用Babel和Grunt正确编译项目

我正在尝试使用Babel,但它对我来说效果不佳.

我的项目很简单

|-project/
|---src/
|-----index.html
|-----main.js
|-----module.js
|---Gruntfile.js
|---package.json
Run Code Online (Sandbox Code Playgroud)

的index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Test</title>
    <script src="main.js" type="application/javascript"></script>
</head>
<body>
<p>Simple html file.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

main.js

import * as math from "./module";

async function anwser() {
    return 42;
}

(function main() {
    anwser().then((v) => {
        console.info(v);
    });

    console.log(math.sum(5, 5));
})();
Run Code Online (Sandbox Code Playgroud)

module.js

export function sum(x, y) {
    return x + y;
}
Run Code Online (Sandbox Code Playgroud)

Gruntfile.js

module.exports = function(grunt) {

    grunt.initConfig({
        "babel": {
            "options": {
                "sourceMap": true,
                "experimental": true
            },
            dist: …
Run Code Online (Sandbox Code Playgroud)

javascript gruntjs babeljs

32
推荐指数
3
解决办法
3万
查看次数

当我尝试sudo gem install json时,我收到以下错误

我需要安装JSON gem来启动我的应用程序,但每当我尝试安装json gem时,我都会收到错误.谁能帮我吗.我正在使用rails 2.2.2和gem 1.3.1.

Arions-macbook-pro:.ssh arion$ sudo gem install json
Password:
Sorry, try again.
Password:
Building native extensions.  This could take a while...
ERROR:  Error installing json:
    ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install json
can't find header files for ruby.


Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/json-1.1.4 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/json-1.1.4/ext/json/ext/parser/gem_make.out
Run Code Online (Sandbox Code Playgroud)

ruby macos json rubygems

30
推荐指数
3
解决办法
4万
查看次数

删除<li>缩进

我已经删除了我的列表上的子弹

ul li { list-style: none; }
Run Code Online (Sandbox Code Playgroud)

但是我仍然在列表上缩进,好像子弹在那里一样.

在此输入图像描述

我需要图像中的列表项准确居中.

html css

29
推荐指数
1
解决办法
5万
查看次数

sed -i选项有什么作用?

我正在调试shell脚本并尝试找出以下命令执行的任务:

sed -i '1,+999d' /home/org_user/data.txt
Run Code Online (Sandbox Code Playgroud)

我需要更改此命令,因为它失败并出现以下错误:

illegal option sed -i
Run Code Online (Sandbox Code Playgroud)

但在改变之前,我需要了解BAU的功能.

感谢这方面的任何投入.

unix command-line sed option

29
推荐指数
3
解决办法
4万
查看次数

node.js表示createServer()不是函数

我正在尝试建立一个快速项目.在我安装npm之后,创建server.js和node server.js,我收到此错误:

var app = module.exports = express.createServer();                                ^
TypeError: undefined is not a function

at Object.<anonymous> (/Users/zez/Desktop/node/nodeblog/server.js:3:36)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:349:32)
at Function.Module._load (module.js:305:12)
at Function.Module.runMain (module.js:490:10)
at startup (node.js:124:16)
at node.js:807:3
Run Code Online (Sandbox Code Playgroud)

这是我在server.js中的代码:

var express = require('express');
var app = module.exports = express.createServer();                                   
app.configure(function(){
app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
});


app.get('/', function(req, res){
    res.render('index', {
            title: 'Express'
                });
});

app.listen(3000);
Run Code Online (Sandbox Code Playgroud)

express

29
推荐指数
3
解决办法
5万
查看次数

如何在Linux下运行C#项目

你知道在Linux下运行C#项目的任何方法吗?是否有任何framewoks或库?

.net c# linux

26
推荐指数
2
解决办法
4万
查看次数

使用Amazon Appstore跟踪Android安装引荐

我目前正在跟踪Android Market和Google Analytics的推荐人,如下所述:http: //code.google.com/mobile/analytics/docs/android/#android-market-tracking

是否有类似的解决方案可用于Amazon Appstore?

android amazon referrer amazon-appstore google-play

26
推荐指数
1
解决办法
1539
查看次数

Http发布并获得角度6的请求

在角度5.2.x的http get和post我有这个代码:

post(url: string, model: any): Observable<boolean> {

return this.http.post(url, model)
  .map(response => response)
  .do(data => console.log(url + ': ' + JSON.stringify(data)))
  .catch(err => this.handleError(err));
 }
 get(url: string): Observable<any> {

return this.http.get(url)
  .map(response => response)
  .do(data =>
    console.log(url + ': ' + JSON.stringify(data))
  )
  .catch((error: any) => Observable.throw(this.handleError(error)));
 }
Run Code Online (Sandbox Code Playgroud)

在角度6中,它不起作用.

我们如何发布HTTP帖子或获取请求?

httprequest observable angular6

26
推荐指数
1
解决办法
10万
查看次数

drawImage并调整大小到Canvas

我有一个1836 x 3264的图像我想drawImage()画布并调整大小为739 x 1162.

阅读完文档后,我认为可以通过以下方式完成:

ctx.drawImage(image, 0, 0, 739, 1162);
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

ctx.drawImage(image, 0, 0, 1836, 3264, 0, 0, 739, 1162);
Run Code Online (Sandbox Code Playgroud)

两者都只显示完整图像的一小部分而不是缩小它.

如何通过值调整大小从1836 x 3264 - > 739 x 1162?

javascript html5-canvas

25
推荐指数
1
解决办法
6万
查看次数