Skip to content

Instantly share code, notes, and snippets.

View xigang's full-sized avatar
🎾
/*happy coding*/

Xigang Wang xigang

🎾
/*happy coding*/
View GitHub Profile
@xigang
xigang / main.go
Created April 22, 2019 05:03
Compress memory usage, no cache
package main
import (
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func main() {
@xigang
xigang / tls.go
Created September 29, 2017 01:58
func createClientConfig(ca, crt, key string) (*tls.Config, error) {
caCertPEM, err := ioutil.ReadFile(ca)
if err != nil {
return nil, err
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(caCertPEM)
if !ok {
panic("failed to parse root certificate")
@xigang
xigang / consistent_hash.go
Created June 13, 2017 06:11
consistent hash algorithm
/*
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
这两个函数是针对单个rune和[]byte之间的转换。
utf8编码时,一个字符可能需要1、2、3或4个字节表示;在go中,一个utf8字符用rune类型表示;所以,这里的Encode和Decode是针对一个rune到[]byte的转换。
如下代码示例:
package main
import (
"fmt"
func RecordAlertHistory(q *Queue, ah *AlertHistory) {
q.PushQueue(ah)
c := make(chan int)
currentQueueLen := q.GetQueueLength()
beego.Debug("Get Queue Len AAA: ", currentQueueLen)
wg.Add(2)
go func() {
@xigang
xigang / sort_time.go
Created May 4, 2017 04:04
Sorting by time.Time in Golang
package main
import (
"fmt"
"sort"
"time"
)
type reviews_data struct {
review_id string
@xigang
xigang / sort_time.go
Last active May 17, 2017 05:48
Sorting by time.Time in Golang
package main
import (
"fmt"
"sort"
"time"
)
type reviews_data struct {
review_id string
@xigang
xigang / generateID.go
Created April 24, 2017 07:31
copy from docker
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/docker/docker/pkg/random"
"io"
"strconv"
"strings"
@xigang
xigang / interface.go
Created April 20, 2017 15:15
golang how to use interface
package main
import (
"fmt"
)
type StorageLogic interface {
Create(id int32) string
Delete(id int32)
}