在nodejs中,我使用__dirname.在Golang中相当于什么?
我用Google搜索并发现了这篇文章http://andrewbrookins.com/tech/golang-get-directory-of-the-current-file/.他使用以下代码的地方
_, filename, _, _ := runtime.Caller(1)
f, err := os.Open(path.Join(path.Dir(filename), "data.csv"))
Run Code Online (Sandbox Code Playgroud)
但这是在Golang中正确的方式或惯用方式吗?
// synchronous Javascript
var result = db.get('select * from table1');
console.log('I am syncronous');
// asynchronous Javascript
db.get('select * from table1', function(result){
// do something with the result
});
console.log('I am asynchronous')
Run Code Online (Sandbox Code Playgroud)
我知道在同步代码中,console.log()在从db获取结果后执行,而在异步代码中,console.log()在db.get()获取结果之前执行.
现在我的问题是,异步代码的幕后执行是如何发生的,为什么它是非阻塞的?
我已经搜索了Ecmascript 5标准,以了解异步代码如何工作,但在整个标准中找不到异步这个词.
从nodebeginner.org我也发现我们不应该使用return语句,因为它阻止了事件循环.但是nodejs api和第三方模块在任何地方都包含return语句.那么什么时候应该使用return语句,何时不应该使用return语句?
有人可以对此有所了解吗?
如何将以下代码转换为使用流/管道,以便我不需要将完整内容读入内存?就像是:
http.Get("http://example.com/").Pipe("./data.txt")
package main
import ("net/http";"io/ioutil")
func main() {
resp, err := http.Get("http://example.com/")
check(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
check(err)
err = ioutil.WriteFile("./data.txt", body, 0666)
check(err)
}
func check(e error) {
if e != nil {
panic(e)
}
}
Run Code Online (Sandbox Code Playgroud) 我发现了许多教程,解释了如何使用node.js刮取不需要身份验证/登录的公共网站.
有人可以解释如何使用node.js抓取需要登录的网站吗?
var https = require('https');
var p = '/api/username/FA/AA?ZOHO_ACTION=EXPORT&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_KEY=dummy1234&ticket=dummy9876&ZOHO_API_VERSION=1.0';
var https = require('https');
var options = {
host: 'reportsapi.zoho.com',
port: 443,
path: p,
method: 'POST'
};
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我得到以下错误.
错误信息:
statusCode: 411
headers: { 'content-type': 'text/html',
'content-length': '357',
connection: 'close',
date: 'Thu, 24 Nov 2011 19:58:51 GMT',
server: 'ZGS',
'strict-transport-security': 'max-age=604800' }
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
411 - Length Required
Run Code Online (Sandbox Code Playgroud)
如何修复abobe错误?
我试过在下面做 …
如何为rethinkdb连接添加用户名和密码?
在我的网络中有100多台机器.其中只有2台机器在我的控制之下.2个中,一个是虚拟机,一个是物理机.
如果我rethinkdb使用以下命令在虚拟机上运行
rethinkdb --bind all
Run Code Online (Sandbox Code Playgroud)
现在我rethinkdb http:// ip:8080接触到网络中的所有机器.如何设置用户名和密码来限制他人?
如果我localhost:8080在虚拟机中使用,则管理HTTP连接是无用的,因为它既不能在物理机器上也不在虚拟机上访问.
我该如何解决这个问题?
谢谢
XML到Json
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
)
type Persons struct {
Person []struct {
Name string
Age int
}
}
type Places struct {
Place []struct {
Name string
Country string
}
}
type Parks struct {
Park struct {
Name []string
Capacity []int
}
}
const personXml = `
<Persons>
<Person><Name>Koti</Name><Age>30</Age></Person>
<Person><Name>Kanna</Name><Age>29</Age></Person>
</Persons>
`
const placeXml = `
<Places>
<Place><Name>Chennai</Name><Country>India</Country></Place>
<Place><Name>London</Name><Country>UK</Country></Place>
</Places>
`
const parkXml = `
<Parks>
<Park><Name>National Park</Name><Capacity>10000</Capacity></Park>
<Park>Asian Park</Name><Capacity>20000</Capacity></Park>
</Parks>
`
func WhatIamUsing() { …Run Code Online (Sandbox Code Playgroud) 我该如何修复错误?
http://play.golang.org/p/0UMnUZOUHw
// JSON to CSV in Golang
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type Json struct {
RecordID int64 `json:"recordId"`
DOJ string `json:"Date of joining"`
EmpID string `json:"Employee ID"`
}
func main() {
// reading data from JSON File
data, err := ioutil.ReadFile("./people.json")
if err != nil {
fmt.Println(err)
}
// Unmarshal JSON data
var d []Json
err = json.Unmarshal([]byte(data), &d)
if err != nil {
fmt.Println(err)
}
// Create a csv file
f, _ …Run Code Online (Sandbox Code Playgroud) 以下是我在github.com/rethinkdb问题上提出的问题.我在这里发布了rethinkdb社区的好处
1.如何根据以下数据过滤最大日期:
[
{"TimeStamp": Fri Oct 11 2013 05:51:12 GMT+00:00},
{"TimeStamp": Thu Oct 10 2013 15:41:09 GMT+00:00},
{"TimeStamp": Thu Oct 10 2013 15:44:04 GMT+00:00}
]
Run Code Online (Sandbox Code Playgroud)
2.如何创建计算字段?我在rethinkdb中有这样的数据
[
{id: 1, sales: 1000, discount: 0.1},
{id: 2, sales: 2000, discount: 0.2},
{id: 3, sales:3000, discount: 0.1}
]
Run Code Online (Sandbox Code Playgroud)
我怎样才能改变如下:
[{id: 1, sales: 1000, discount: 0.1, discAmt: 100, netSales: 900},
{id: 2, sales: 2000, discount: 0.2, discAmt: 400, netSales: 1600},
{id: 3, sales: 3000, discount: 0.1, discAmt: 300, netSales: 2700}
]
Run Code Online (Sandbox Code Playgroud)
如何从上面删除折扣字段?
go ×4
javascript ×3
node.js ×2
rethinkdb ×2
asynchronous ×1
execution ×1
function ×1
post ×1
request ×1
return ×1
web-scraping ×1