當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。