我正在使用golang和阅读本文档的 mysql .它说
尽管在完成数据库时Close()数据库是惯用的,但sql.DB对象的设计是长期存在的.不经常打开()和关闭()数据库.
我不知道连接应该有多长时间.现在,如有必要,我会打开每个http请求的连接.太频繁了吗?
客户端发送文件,大小可能超过5G,到从服务器,而不是从服务器发送到主服务器.
奴隶会将临时文件保存到自身吗?我不希望它发生,因为它会降低上传速度并浪费奴隶的记忆.
有什么办法可以避免这个吗 在golang中传输大文件的最佳方法是什么?
我的TypeScript版本是2.0.10。
组件
import * as React from "react";
export interface HelloProps { list?: number[]; }
export class Hello extends React.Component<HelloProps, {}> {
static defaultProps = {
list: [1, 2, 3]
}
static propTypes = {
list: React.PropTypes.array,
title: React.PropTypes.string
};
render() {
let {list} = this.props
return (
<ul>
{
// error here: Object is possibly 'undefined'.
list.map(item => (
<li>{item}</li>
))
}
</ul>
)
}
}
Run Code Online (Sandbox Code Playgroud)
TypeScript编译器配置文件
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": …Run Code Online (Sandbox Code Playgroud) 我使用io.Copy()来复制一个大约700Mb的文件,但它会导致内存不足
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
//key step
fileWriter, err := bodyWriter.CreateFormFile(paramName, fileName)
if err != nil {
return nil, err
}
file, err := os.Open(fileName) //the file size is about 700Mb
if err != nil {
return nil, err
}
defer file.Close()
//iocopy
copyLen, err := io.Copy(fileWriter, file) // this cause out of memory
if err != nil {
fmt.Println("io.copy(): ", err)
return nil, err
}
Run Code Online (Sandbox Code Playgroud)
错误消息如下:
runtime: memory allocated by OS (0x752cf000) not in usable range [0x18700000,0x98700000) …Run Code Online (Sandbox Code Playgroud) package main
import (
"fmt"
"io"
"io/ioutil"
"os"
)
func main() {
file, err := os.Open("HelloWorld")
if nil != err {
fmt.Println(err)
}
defer file.Close()
fileTo, err := os.Create("fileTo")
if nil != err {
fmt.Println(err)
}
defer file.Close()
_, err = io.Copy(fileTo, file)
if nil != err {
fmt.Println(err)
}
fileByteOne, err := ioutil.ReadAll(file)
if nil != err {
fmt.Println(err)
}
fmt.Println(fileByteOne)
}
Run Code Online (Sandbox Code Playgroud)
io.Copy() 将删除文件内容,输出为:
[]
Run Code Online (Sandbox Code Playgroud)
Copy(dst Writer, src Reader) 从 src 复制到 dst,会擦除 src 内容。有什么办法可以避免被删除吗?
......
resp, err := httplib.Get(url)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
......
Run Code Online (Sandbox Code Playgroud)
是否有必要每次关闭响应机构?