我正在尝试使用Golang写入日志文件.
我尝试了几种方法,但都失败了.这是我尝试过的:
func TestLogging(t *testing.T) {
if !FileExists("logfile") {
CreateFile("logfile")
}
f, err := os.Open("logfile")
if err != nil {
t.Fatalf("error: %v", err)
}
// attempt #1
log.SetOutput(io.MultiWriter(os.Stderr, f))
log.Println("hello, logfile")
// attempt #2
log.SetOutput(io.Writer(f))
log.Println("hello, logfile")
// attempt #3
log.SetOutput(f)
log.Println("hello, logfile")
}
func FileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func CreateFile(name string) error {
fo, err := os.Create(name)
if err != …Run Code Online (Sandbox Code Playgroud) 在Go/GoLang中,检查IP地址是否在特定范围内的最快方法是什么?
例如,给定的范围216.14.49.184来216.14.49.191,我将如何检查是否给定的输入的IP地址是在这个范围内?
我创建了一个ASP.NET Web API控制器,它在一个动作上返回一个强类型对象,如下所示:
// GET api/iosdevices/5
public iOSDevice Get(string id) {
return new iOSDevice();
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个BufferedMediaTypeFormatter来处理iOSDevice类型:
public class iOSDeviceXmlFormatter : BufferedMediaTypeFormatter
{
public iOSDeviceXmlFormatter() {
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content) {
content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
iOSDevice device = (iOSDevice)value;
using (XmlWriter writer = XmlWriter.Create(writeStream)) {
writer.WriteStartElement("iOSDevice");
if (device.identifierForVendor != Guid.Empty) {
writer.WriteElementString("identifierForVendor", device.identifierForVendor.ToString());
writer.WriteElementString("userInterfaceIdiom", device.userInterfaceIdiom);
writer.WriteElementString("systemName", device.systemName);
writer.WriteElementString("systemVersion", device.systemVersion);
writer.WriteElementString("model", device.model);
}
writer.WriteEndElement();
}
writeStream.Close();
}
} …Run Code Online (Sandbox Code Playgroud) 这是在Ubuntu 12.04.3 LTS服务器上.
我已将以下内容添加到/etc/security/limits.conf(我的Golang进程以root身份运行):
* hard nofile 50000
* soft nofile 50000
root hard nofile 50000
root soft nofile 50000
Run Code Online (Sandbox Code Playgroud)
我在/etc/pam.d/common-session中添加了以下内容
session required pam_limits.so
Run Code Online (Sandbox Code Playgroud)
我已将以下内容添加到/etc/sysctl.conf中:
fs.file-max = 50000
Run Code Online (Sandbox Code Playgroud)
然而当我cat/proc/{PID} /限制时,我得到:
Limit Soft Limit Hard Limit Units
Max open files 1024 4096 files
Run Code Online (Sandbox Code Playgroud)
只有当我从Upstart通过sudo initctl start service_name启动进程时才会发生这种情况.如果我自己开始这个过程,它会确认我的设置.
我该如何解决?
我正在使用Redis网站上建议的Redigo连接器(https://github.com/garyburd/redigo)在Golang中使用redis .
我有:
我运行了一个高流量的网站,一切都运行了大约10分钟,我得到了
error: dial tcp 127.0.0.1:6379: too many open files
Run Code Online (Sandbox Code Playgroud)
然后我无法从我的应用程序访问redis.
我在redis日志中看不到任何错误或问题.我该怎么做才能解决这个问题?
sql.Open()返回*sql.DB类型的变量
我有一个函数调用其他所有需要进行数据库调用的函数
是否更正确/更有效:
含义
func DoLotsOfThings() {
db, _ := sql.Open()
defer db.Close()
DoTask1(db)
DoTask2(db)
}
Run Code Online (Sandbox Code Playgroud)
要么
func DoLotsOfThings() {
DoTask1()
DoTask2()
}
func DoTask1() {
db, _ := sql.Open()
defer db.Close()
}
func DoTask1() {
db, _ := sql.Open()
defer db.Close()
}
Run Code Online (Sandbox Code Playgroud)
我问的原因是因为我正在发送指向每个函数的指针,而我的驱动程序似乎打破了.我正在使用http://code.google.com/p/odbc,这让我相信每个功能都应该有自己的功能,并且我可以依赖于驱动程序的内部功能.
编辑
RE驱动器破损,它只发生在高流量环境下.它只发生在十分钟左右的时间之后.这让我相信存在某种内存泄漏会导致使用驱动程序停止工作.但是我为*sql.DB的每个实例推迟db.Close(),所以我不知道我还能做些什么来解决这个问题.
andybalholm说连接池是内部处理的,这似乎是准确的,因为它只在我尝试执行某些东西后才会中断,而不是在我调用sql.Open()时
如果我让Go应用程序运行,它将无法执行任何类型的SQL查询,但如果我尝试单独运行其他Go测试连接到MSSQL并运行查询,它就可以运行.
我已经查看了Xamarin的文档,并建议使用ThreadPool来实现多线程功能,如下所示:
http://docs.xamarin.com/guides/ios/application_fundamentals/threading
但是,已经完成了一项基准测试,表明Grand Central Dispatch比ThreadPool更具性能
因此,我的问题是,为什么Xamarin推荐ThreadPool而不是Grand Central Dispatch?Xamarin最终是否会将ThreadPool绑定到Grand Central Dispatch?什么时候选择一个而不是另一个?因为如果ThreadPool将由Xamarin优化,并最终胜过Grand Central Dispatch,那么我不想使用Grand Central Dispatch.
我有一个利用MySQL会话变量的查询(注意@rank变量)
SELECT Rank, UserId, CurrentVDOT
FROM
(
SELECT @rank := @rank + 1 AS Rank, UserId, MaxVDOT AS CurrentVDOT
FROM
(
SELECT UserId, MAX(VDOT) AS MaxVDOT
FROM
(
SELECT U.UserId, U.VDOT
FROM
(
SELECT UserId, MAX(Created) AS Created
FROM UserVDOT
GROUP BY UserId
) G
INNER JOIN UserVDOT U
ON U.UserId = G.UserId
AND U.Created = G.Created
) M
GROUP BY UserId
ORDER BY MaxVDOT DESC
) R, (SELECT @rank := 0) foo
) F
WHERE F.UserId = @UserId;
Run Code Online (Sandbox Code Playgroud)
如果我尝试对C#MySQL连接器执行此操作,它会尝试告诉我需要将@rank声明为输入参数变量. …
go ×4
c# ×3
asp.net ×2
freetds ×1
ip-address ×1
logging ×1
monodevelop ×1
mysql ×1
odbc ×1
redis ×1
ubuntu ×1
unixodbc ×1
upstart ×1
xamarin ×1
xamarin.ios ×1