本文整理匯總了Golang中github.com/layer-x/layerx-commons/lxerrors.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PostAsyncFile
func PostAsyncFile(url, path, fileKey, pathToFile string) (*http.Response, error) {
completeURL := parseURL(url, path)
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
// this step is very important
fileWriter, err := bodyWriter.CreateFormFile(fileKey, pathToFile)
if err != nil {
return nil, lxerrors.New("error writing to buffer", err)
}
// open file handle
fh, err := os.Open(pathToFile)
if err != nil {
return nil, lxerrors.New("error opening file", err)
}
//iocopy
_, err = io.Copy(fileWriter, fh)
if err != nil {
return nil, lxerrors.New("error copying file to form", err)
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post(completeURL, contentType, bodyBuf)
if err != nil {
return resp, lxerrors.New("error performing post", err)
}
return resp, nil
}
示例2: Respond
func Respond(res http.ResponseWriter, message interface{}) error {
switch message.(type) {
case string:
messageString := message.(string)
data := []byte(messageString)
_, err := res.Write(data)
if err != nil {
return lxerrors.New("writing data", err)
}
return nil
case error:
responseError := message.(error)
http.Error(res, responseError.Error(), http.StatusInternalServerError)
return nil
}
data, err := json.Marshal(message)
if err != nil {
return lxerrors.New("marshalling message to json", err)
}
_, err = res.Write(data)
if err != nil {
return lxerrors.New("writing data", err)
}
return nil
}
示例3: postData
func postData(url string, path string, headers map[string]string, data []byte) (*http.Response, []byte, error) {
completeURL := parseURL(url, path)
request, err := http.NewRequest("POST", completeURL, bytes.NewReader(data))
if err != nil {
return nil, emptyBytes, lxerrors.New("error generating post request", err)
}
if auth != nil {
request.SetBasicAuth(auth.username, auth.password)
auth = nil
}
for key, value := range headers {
request.Header.Add(key, value)
}
resp, err := newClient().c.Do(request)
if err != nil {
return resp, emptyBytes, lxerrors.New("error performing post request", err)
}
respBytes, err := ioutil.ReadAll(resp.Body)
if resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
return resp, emptyBytes, lxerrors.New("error reading post response", err)
}
return resp, respBytes, nil
}
示例4: deleteAsyncWithRetries
func deleteAsyncWithRetries(url string, path string, headers map[string]string, retries int) (*http.Response, error) {
resp, err := func() (*http.Response, error) {
completeURL := parseURL(url, path)
request, err := http.NewRequest("DELETE", completeURL, nil)
if err != nil {
return nil, lxerrors.New("error generating delete request", err)
}
if auth != nil {
request.SetBasicAuth(auth.username, auth.password)
auth = nil
}
for key, value := range headers {
request.Header.Add(key, value)
}
resp, err := newClient().c.Do(request)
if err != nil {
return resp, lxerrors.New("error performing delete request", err)
}
return resp, nil
}()
if err != nil && retries > 0 {
return deleteAsyncWithRetries(url, path, headers, retries-1)
}
return resp, err
}
示例5: getWithRetries
func getWithRetries(url string, path string, headers map[string]string, retries int) (*http.Response, []byte, error) {
resp, respBytes, err := func() (*http.Response, []byte, error) {
completeURL := parseURL(url, path)
request, err := http.NewRequest("GET", completeURL, nil)
if err != nil {
return nil, emptyBytes, lxerrors.New("error generating get request", err)
}
if auth != nil {
request.SetBasicAuth(auth.username, auth.password)
auth = nil
}
for key, value := range headers {
request.Header.Add(key, value)
}
resp, err := newClient().c.Do(request)
if err != nil {
return resp, emptyBytes, lxerrors.New("error performing get request", err)
}
respBytes, err := ioutil.ReadAll(resp.Body)
if resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
return resp, emptyBytes, lxerrors.New("error reading get response", err)
}
return resp, respBytes, nil
}()
if err != nil && retries > 0 {
return getWithRetries(url, path, headers, retries-1)
}
return resp, respBytes, err
}
示例6: Get
func Get(key string) (string, error) {
m.Lock()
defer m.Unlock()
key = prefixKey(key)
kapi := client.NewKeysAPI(c)
resp, err := kapi.Get(context.Background(), key, nil)
if err != nil {
return "", lxerrors.New("getting key/val", err)
}
if resp.Node.Dir {
return "", lxerrors.New("get used on a dir", err)
}
return resp.Node.Value, nil
}
示例7: Rm
func Rm(key string) error {
m.Lock()
defer m.Unlock()
key = prefixKey(key)
kapi := client.NewKeysAPI(c)
resp, err := kapi.Delete(context.Background(), key, nil)
if err != nil {
return lxerrors.New("deleting key/val pair", err)
}
if resp.Node.Key != key {
return lxerrors.New("removed pair does not have expected key", nil)
}
return nil
}
示例8: Set
func Set(key string, value string) error {
m.Lock()
defer m.Unlock()
key = prefixKey(key)
kapi := client.NewKeysAPI(c)
resp, err := kapi.Set(context.Background(), key, value, nil)
if err != nil {
return lxerrors.New("setting key/val pair", err)
}
if resp.Node.Key != key || resp.Node.Value != value {
fmt.Printf("key was %s, value was %s", resp.Node.Key, resp.Node.Value)
return lxerrors.New("key/value pair not set as expected", nil)
}
return nil
}
示例9: postAsyncPB
func postAsyncPB(url string, path string, headers map[string]string, pb proto.Message) (*http.Response, error) {
data, err := proto.Marshal(pb)
if err != nil {
return nil, lxerrors.New("could not proto.Marshal mesasge", err)
}
return postAsyncData(url, path, headers, data)
}
示例10: GetLocalIp
func GetLocalIp() (net.IP, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
// handle err
for _, i := range ifaces {
if i.Name == "eth1" {
addrs, err := i.Addrs()
if err != nil {
return nil, err
}
// handle err
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
return v.IP, nil
case *net.IPAddr:
return v.IP, nil
}
// process IP address
}
}
}
return nil, lxerrors.New("Could not find IP in network interfaces", nil)
}
示例11: Mkdir
func Mkdir(dir string) error {
m.Lock()
defer m.Unlock()
dir = prefixKey(dir)
kapi := client.NewKeysAPI(c)
opts := &client.SetOptions{
Dir: true,
}
resp, err := kapi.Set(context.Background(), dir, "", opts)
if err != nil {
return lxerrors.New("making directory", err)
}
if resp.Node.Key != dir || !resp.Node.Dir {
return lxerrors.New("directory not created as expected", nil)
}
return nil
}
示例12: Rmdir
func Rmdir(dir string, recursive bool) error {
m.Lock()
defer m.Unlock()
dir = prefixKey(dir)
kapi := client.NewKeysAPI(c)
opts := &client.DeleteOptions{
Dir: true,
Recursive: recursive,
}
resp, err := kapi.Delete(context.Background(), dir, opts)
if err != nil {
return lxerrors.New("removing directory", err)
}
if resp.Node.Key != dir || !resp.Node.Dir {
return lxerrors.New("directory not created as expected", nil)
}
return nil
}
示例13: UntarFileToDirectory
func UntarFileToDirectory(targetDirectory string, sourceTar multipart.File, header *multipart.FileHeader) (int64, error) {
savedTar, err := os.OpenFile(targetDirectory+filepath.Base(header.Filename), os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return 0, lxerrors.New("creating empty file for copying to", err)
}
defer savedTar.Close()
bytesWritten, err := io.Copy(savedTar, sourceTar)
if err != nil {
return 0, lxerrors.New("copying uploaded file to disk", err)
}
err = Untar(savedTar.Name(), targetDirectory)
if err != nil {
err = UntarNogzip(savedTar.Name(), targetDirectory)
if err != nil {
return 0, lxerrors.New("untarring saved tar", err)
}
}
return bytesWritten, nil
}
示例14: postAsyncBuffer
func postAsyncBuffer(url string, path string, headers map[string]string, reader io.Reader) (*http.Response, error) {
completeURL := parseURL(url, path)
request, err := http.NewRequest("POST", completeURL, reader)
if err != nil {
return nil, lxerrors.New("error generating post request", err)
}
for key, value := range headers {
request.Header.Add(key, value)
}
if auth != nil {
request.SetBasicAuth(auth.username, auth.password)
auth = nil
}
resp, err := newClient().c.Do(request)
if err != nil {
return resp, lxerrors.New("error performing post request", err)
}
return resp, nil
}
示例15: GetSubdirectories
func GetSubdirectories(dir string) ([]string, error) {
m.Lock()
defer m.Unlock()
dir = prefixKey(dir)
kapi := client.NewKeysAPI(c)
resp, err := kapi.Get(context.Background(), dir, nil)
if err != nil {
return []string{}, lxerrors.New("getting key/vals for dir", err)
}
if !resp.Node.Dir {
return []string{}, lxerrors.New("ls used on a non-dir key", err)
}
result := []string{}
for _, node := range resp.Node.Nodes {
if node.Dir {
result = append(result, node.Key)
} //ignore keys
}
return result, nil
}