当前位置: 首页>>代码示例>>Golang>>正文


Golang strings.Bytes函数代码示例

本文整理汇总了Golang中strings.Bytes函数的典型用法代码示例。如果您正苦于以下问题:Golang Bytes函数的具体用法?Golang Bytes怎么用?Golang Bytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Bytes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: main

func main() {
	addr, err := net.ResolveTCPAddr("127.0.0.1:4009")
	if err != nil {
		log.Exit("error:", err)
	}
	listener, err := net.ListenTCP("tcp", addr)
	if err != nil {
		log.Exit("error", err)
	}

	//1 channel for incoming connections, another for client communication
	connections := make(chan *net.TCPConn)
	clients := make(chan TClient)
	cMap := make(map[string]*net.TCPConn)
	fMap := make(map[string]string)

	go ListenConnections(listener, connections, clients)
	log.Stdout("Waiting for connections\n")
	for {
		select {
		case conn := <-connections:
			cMap[conn.RemoteAddr().String()] = conn
		case client := <-clients:
			if regexp.MustCompile("^have ").MatchString(client.msg) {
				fMap[string(client.msg[5:len(client.msg)])] = client.local
			}
			if regexp.MustCompile("^list").MatchString(client.msg) {
				for key, value := range fMap {
					cMap[client.forserver].Write(strings.Bytes(key + "->" + value))
				}
				cMap[client.forserver].Write(strings.Bytes("\n"))
			}
		}
	}
}
开发者ID:WalterShe,项目名称:gop2p,代码行数:35,代码来源:server.go

示例2: TestList

func TestList(t *testing.T) {
	//var err os.Error

	vals := []string{"a", "b", "c", "d", "e"}

	for _, v := range vals {
		client.Rpush("l", strings.Bytes(v))
	}

	if l, err := client.Llen("l"); err != nil || l != 5 {
		t.Fatal("Llen failed", err.String())
	}

	for i := 0; i < len(vals); i++ {
		if val, err := client.Lindex("l", i); err != nil || string(val) != vals[i] {
			t.Fatal("Lindex failed", err.String())
		}
	}

	for i := 0; i < len(vals); i++ {
		if err := client.Lset("l", i, strings.Bytes("a")); err != nil {
			t.Fatal("Lset failed", err.String())
		}
	}

	for i := 0; i < len(vals); i++ {
		if val, err := client.Lindex("l", i); err != nil || string(val) != "a" {
			t.Fatal("Lindex failed", err.String())
		}
	}

	client.Del("l")

}
开发者ID:Goemon,项目名称:redis.go,代码行数:34,代码来源:redis_test.go

示例3: Rename

// Redis RENAME command.
func (c *syncClient) Rename(arg0 string, arg1 string) (err Error) {
	arg0bytes := strings.Bytes(arg0)
	arg1bytes := strings.Bytes(arg1)

	_, err = c.conn.ServiceRequest(&RENAME, [][]byte{arg0bytes, arg1bytes})
	return
}
开发者ID:nono,项目名称:Go-Redis,代码行数:8,代码来源:synchclient.go

示例4: TestWriter

// Tests that gzipping and then gunzipping is the identity function.
func TestWriter(t *testing.T) {
	pipe(t,
		func(deflater *Deflater) {
			deflater.Comment = "comment"
			deflater.Extra = strings.Bytes("extra")
			deflater.Mtime = 1e8
			deflater.Name = "name"
			_, err := deflater.Write(strings.Bytes("payload"))
			if err != nil {
				t.Fatalf("%v", err)
			}
		},
		func(inflater *Inflater) {
			b, err := ioutil.ReadAll(inflater)
			if err != nil {
				t.Fatalf("%v", err)
			}
			if string(b) != "payload" {
				t.Fatalf("payload is %q, want %q", string(b), "payload")
			}
			if inflater.Comment != "comment" {
				t.Fatalf("comment is %q, want %q", inflater.Comment, "comment")
			}
			if string(inflater.Extra) != "extra" {
				t.Fatalf("extra is %q, want %q", inflater.Extra, "extra")
			}
			if inflater.Mtime != 1e8 {
				t.Fatalf("mtime is %d, want %d", inflater.Mtime, uint32(1e8))
			}
			if inflater.Name != "name" {
				t.Fatalf("name is %q, want %q", inflater.Name, "name")
			}
		})
}
开发者ID:lougxing,项目名称:golang-china,代码行数:35,代码来源:gzip_test.go

示例5: TestSet

func TestSet(t *testing.T) {
	var err os.Error

	vals := []string{"a", "b", "c", "d", "e"}

	for _, v := range vals {
		client.Sadd("s", strings.Bytes(v))
	}

	var members [][]byte

	if members, err = client.Smembers("s"); err != nil || len(members) != 5 {
		t.Fatal("Set setup failed", err.String())
	}

	for _, v := range vals {
		if ok, err := client.Sismember("s", strings.Bytes(v)); err != nil || !ok {
			t.Fatal("Sismember test failed")
		}
	}

	for _, v := range vals {
		if ok, err := client.Srem("s", strings.Bytes(v)); err != nil || !ok {
			t.Fatal("Sismember test failed")
		}
	}

	if members, err = client.Smembers("s"); err != nil || len(members) != 0 {
		t.Fatal("Set setup failed", err.String())
	}

	client.Del("s")

}
开发者ID:Goemon,项目名称:redis.go,代码行数:34,代码来源:redis_test.go

示例6: TestPartialRead

func TestPartialRead(t *testing.T) {
	f, err := os.Open("testdata/gnu.tar", os.O_RDONLY, 0444)
	if err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	defer f.Close()

	tr := NewReader(f)

	// Read the first four bytes; Next() should skip the last byte.
	hdr, err := tr.Next()
	if err != nil || hdr == nil {
		t.Fatalf("Didn't get first file: %v", err)
	}
	buf := make([]byte, 4)
	if _, err := io.ReadFull(tr, buf); err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	if expected := strings.Bytes("Kilt"); !bytes.Equal(buf, expected) {
		t.Errorf("Contents = %v, want %v", buf, expected)
	}

	// Second file
	hdr, err = tr.Next()
	if err != nil || hdr == nil {
		t.Fatalf("Didn't get second file: %v", err)
	}
	buf = make([]byte, 6)
	if _, err := io.ReadFull(tr, buf); err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	if expected := strings.Bytes("Google"); !bytes.Equal(buf, expected) {
		t.Errorf("Contents = %v, want %v", buf, expected)
	}
}
开发者ID:lougxing,项目名称:golang-china,代码行数:35,代码来源:reader_test.go

示例7: TestGetByte

func TestGetByte(t *testing.T) {
	if b, _ := _GetByte(strings.Bytes("\x00\x11"), 1); b != 0x11 {
		t.Errorf("#1 Failed 0x%X != 0x11", b)
	}
	if _, e := _GetByte(strings.Bytes("\x00\x11"), 2); e == nil {
		t.Errorf("#2 Failed")
	}
}
开发者ID:papamitra,项目名称:go-dbus,代码行数:8,代码来源:marshall_test.go

示例8: Ltrim

// Redis LTRIM command.
func (c *syncClient) Ltrim(arg0 string, arg1 int64, arg2 int64) (err Error) {
	arg0bytes := strings.Bytes(arg0)
	arg1bytes := strings.Bytes(fmt.Sprintf("%d", arg1))
	arg2bytes := strings.Bytes(fmt.Sprintf("%d", arg2))

	_, err = c.conn.ServiceRequest(&LTRIM, [][]byte{arg0bytes, arg1bytes, arg2bytes})
	return
}
开发者ID:nono,项目名称:Go-Redis,代码行数:9,代码来源:synchclient.go

示例9: Lset

// Redis LSET command.
func (c *syncClient) Lset(arg0 string, arg1 int64, arg2 []byte) (err Error) {
	arg0bytes := strings.Bytes(arg0)
	arg1bytes := strings.Bytes(fmt.Sprintf("%d", arg1))
	arg2bytes := arg2

	_, err = c.conn.ServiceRequest(&LSET, [][]byte{arg0bytes, arg1bytes, arg2bytes})
	return
}
开发者ID:nono,项目名称:Go-Redis,代码行数:9,代码来源:synchclient.go

示例10: Equal

// compare two clients: name and network connection
func (c *ClientChat) Equal(cl *ClientChat) bool {
	if bytes.Equal(strings.Bytes(c.Name), strings.Bytes(cl.Name)) {
		if c.Con == cl.Con {
			return true
		}
	}
	return false
}
开发者ID:eryx,项目名称:tips.go,代码行数:9,代码来源:chat-server.go

示例11: SaveKeys

func SaveKeys(n, e, d, p, q *big.Int) {
	pu := strings.Bytes(n.String() + "\n" + e.String() + "\n")
	pr := strings.Bytes(p.String() + "\n" + q.String() + "\n" + d.String() + "\n")

	if ioutil.WriteFile(*publicKeyFile, pu, 0600) != nil ||
		ioutil.WriteFile(*privateKeyFile, pr, 0600) != nil {
		panic("Writing problems")
	}
}
开发者ID:JackeLee,项目名称:drobnosti,代码行数:9,代码来源:rsa.go

示例12: checkAppendString

func checkAppendString(t *testing.T, input []string, expected string) {
	buff := bytes.NewBuffer([]byte{})
	for _, str := range input {
		_AppendString(buff, str)
	}
	if !bytes.Equal(strings.Bytes(expected), buff.Bytes()) {
		t.Error("Failed:expected", strings.Bytes(expected), ", actual:", buff.Bytes())
	}
}
开发者ID:papamitra,项目名称:go-dbus,代码行数:9,代码来源:marshall_test.go

示例13: Digest

func Digest(key string, m string) string {
	myhash := hmac.NewSHA1(strings.Bytes(key));
	myhash.Write(strings.Bytes(m));
	signature := bytes.TrimSpace(myhash.Sum());
	digest := make([]byte, base64.StdEncoding.EncodedLen(len(signature)));
	base64.StdEncoding.Encode(digest, signature);
	digest_str := strings.TrimSpace(bytes.NewBuffer(digest).String());
	return digest_str;
}
开发者ID:montsamu,项目名称:go-twitter-oauth,代码行数:9,代码来源:oauth.go

示例14: TestIndex

func TestIndex(t *testing.T) {
	for _, tt := range indextests {
		a := strings.Bytes(tt.a)
		b := strings.Bytes(tt.b)
		pos := Index(a, b)
		if pos != tt.i {
			t.Errorf(`Index(%q, %q) = %v`, tt.a, tt.b, pos)
		}
	}
}
开发者ID:lougxing,项目名称:golang-china,代码行数:10,代码来源:bytes_test.go

示例15: TestGetString

func TestGetString(t *testing.T) {
	s, e := _GetString(strings.Bytes("\x00\x00test"), 2, 4)
	if e != nil || s != "test" {
		t.Error("#1 Failed")
	}
	s, e = _GetString(strings.Bytes("1234"), 3, 1)
	if e != nil || s != "4" {
		t.Error("#2 Failed")
	}
}
开发者ID:papamitra,项目名称:go-dbus,代码行数:10,代码来源:marshall_test.go


注:本文中的strings.Bytes函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。