小编fae*_*ons的帖子

在Golang中检测不同类型网络错误的便携方式

我想确定网络级别发生了什么样的错误.我找到的唯一方法是使用正则表达式检查错误消息,但现在我发现此消息可以使用不同的语言(取决于操作系统配置),因此很难通过正则表达式进行检测.有没有更好的方法呢?

package main

import (
  "github.com/miekg/dns"
  "net"
  "regexp"
)

func main() {
  var c dns.Client
  m := new(dns.Msg)

  m.SetQuestion("3com.br.", dns.TypeSOA)
  _, _, err := c.Exchange(m, "ns1.3com.com.:53")
  checkErr(err)

  m.SetQuestion("example.com.", dns.TypeSOA)
  _, _, err = c.Exchange(m, "idontexist.br.:53")
  checkErr(err)

  m.SetQuestion("acasadocartaocuritiba.blog.br.", dns.TypeSOA)
  _, _, err = c.Exchange(m, "ns7.storedns22.in.:53")
  checkErr(err)
}

func checkErr(err error) {
  if err == nil {
    println("Ok")
  } else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
    println("Timeout")
  } else if match, _ := regexp.MatchString(".*lookup.*", err.Error()); match {
    println("Unknown host") …
Run Code Online (Sandbox Code Playgroud)

error-handling network-programming go

17
推荐指数
1
解决办法
6546
查看次数

Karma/Jasmine测试自定义指令控制器

我正在尝试使用Karma + Jasmine测试AngularJS自定义指令.我找到了一种方法来检查网络上的许多参考文献.但解决方案似乎不是正确的方法.我们先来看一个例子,这是test.js:

angular.module("app", [])
  .directive("test", function() {
    return {
      restrict: 'E',
      scope: {
        defined: '='
      },
      templateFile: "test.html",
      controller: function($scope) {
        $scope.isDefined = function() {
          return $scope.defined;
        };
      }
    };
  });

describe("Test directive", function() {
  var elm, scope;

  beforeEach(module("app"));
  beforeEach(module("test.html"));

  beforeEach(inject(function($rootScope, $compile, $injector) {
    elm = angular.element("<test defined='defined'></test>");

    scope = $rootScope;
    scope.defined = false;

    $compile(elm)(scope);
    scope.$digest();
  }));

  it("should not be initially defined", function() {
    expect(elm.scope().$$childTail.isDefined()).toBe(false);
  });
});
Run Code Online (Sandbox Code Playgroud)

现在指令模板文件test.html:

<button data-ng-click='defined = true'></button>
Run Code Online (Sandbox Code Playgroud)

最后是karma.conf.js:

module.exports = function(config) {
  config.set({
    basePath: …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing jasmine angularjs-directive karma-runner

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

如何正确解析时区代码

在下面的示例中,无论您为parseAndPrint函数选择的时区,结果始终为"[date] 05:00:00 +0000 UTC".这段代码有什么问题?时间应根据您选择的时区而变化.(Go Playground服务器显然是以UTC时区配置的).

http://play.golang.org/p/wP207BWYEd

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    parseAndPrint(now, "BRT")
    parseAndPrint(now, "EDT")
    parseAndPrint(now, "UTC")
}

func parseAndPrint(now time.Time, timezone string) {
    test, err := time.Parse("15:04:05 MST", fmt.Sprintf("05:00:00 %s", timezone))
    if err != nil {
        fmt.Println(err)
        return
    }

    test = time.Date(
        now.Year(),
        now.Month(),
        now.Day(),
        test.Hour(),
        test.Minute(),
        test.Second(),
        test.Nanosecond(),
        test.Location(),
    )

    fmt.Println(test.UTC())
}
Run Code Online (Sandbox Code Playgroud)

time timezone parsing go

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

GoLang - 坚持使用ISO-8859-1 charset

我正在开发一个项目,我们需要将我们的信息保存在具有ISO-8859-1表的遗留数据库中.因此,在向数据库写入内容之前,我需要将其从UTF-8转换为ISO-8859-1,每次从数据库中检索它时,我都需要将其转换回UTF-8.

我试图使用库code.google.com/p/go-charset/作为我需要保留的每个文本字段的以下内容.

import (
  "bytes"
  "code.google.com/p/go-charset/charset"
  _ "code.google.com/p/go-charset/data"
  "fmt"
  "io/ioutil"
  "strings"
)

func toISO88591(utf8 string) string {
    buf := new(bytes.Buffer)

    w, err := charset.NewWriter("latin1", buf)
    if err != nil {
        panic(err)
    }
    defer w.Close()

    fmt.Fprintf(w, utf8)
    return buf.String()
}

func fromISO88591(iso88591 string) string {
    r, err := charset.NewReader("latin1", strings.NewReader(iso88591))
    if err != nil {
        panic(err)
    }

    buf, err := ioutil.ReadAll(r)
    if err != nil {
        panic(err)
    }

    return string(buf)
}
Run Code Online (Sandbox Code Playgroud)

问题是即使我使用函数toISO88591,数据仍然保持在UTF-8中.我在这次转换中做错了什么?

我的数据库是MySQL,我正在使用github.com/go-sql-driver/mysql驱动程序,其中包含以下连接参数:

<user>:<password>@tcp(<host>:<port>)/<database>?collation=latin1_general_ci
Run Code Online (Sandbox Code Playgroud)

最好的祝福!

mysql database utf-8 iso-8859-1 go

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