小编Jak*_*ler的帖子

C# - Resources.GetDrawable在Android Xamarin中已经过时了

Resources.GetDrawable在andrdoid Xamarin中实现了.该程序有效,但如果我点击按钮实现Resources.GetDrawable程序强制关闭.这是我的代码:

SetContentView(Resource.Layout.Main);
drawView = FindViewById<DrawingView>(Resource.Id.drawing);
LinearLayout paintLayout = FindViewById<LinearLayout>(Resource.Id.paint_colors);
currPaint = (ImageButton)paintLayout.GetChildAt(0);
currPaint.SetImageDrawable(Resources.GetDrawable(Resource.Drawable.paint_pressed));
Run Code Online (Sandbox Code Playgroud)

Resources.GetDrawable(Resource.Drawable.paint_pressed得到绿色下划线(在Visual Studio 2015年Xamarin).日志消息返回异常:

Android.Content.Res.Resources + NotFoundException

'Resources.GetDrawable(int)'已过时:'deprecated'

对于Java版本,解决方案是使用以下三种方法之一:

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
//or
ContextCompat.getDrawable(getActivity(), R.drawable.name);
//or
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
Run Code Online (Sandbox Code Playgroud)

C#Android Xamarin的版本是什么?谢谢.

c# android android-layout xamarin.android xamarin

6
推荐指数
3
解决办法
7322
查看次数

Go http 客户端超时与上下文超时

在 http.Client 中设置的超时和在请求上下文中设置的超时有什么区别?

我见过 2 种在 http 客户端设置超时的方法。

第一的:

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080", nil)
Run Code Online (Sandbox Code Playgroud)

第二:

client := http.Client{
    Timeout: 2 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
    panic(err)
}
Run Code Online (Sandbox Code Playgroud)

什么时候使用一个?

http go go-http

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

从 Go 的上下文中获取超时值

如何获取上下文中超时设置的持续时间。

例子:

func f(ctx context.Context) {
    // get ctx timeout value
}

func main() {
    ctx, cancel := context.WithTimeout(context.Background(),2*time.Second)
    defer cancel()
    f(ctx)
}
Run Code Online (Sandbox Code Playgroud)

如何2*time.Second从函数内获取持续时间f

go

4
推荐指数
1
解决办法
3581
查看次数

在Golang中解析转义的json字符串

假设我有这个 json 字符串:

{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\",\"D\":2,\"E\":\"e\"}
Run Code Online (Sandbox Code Playgroud)

我想将上面的字符串转换为结构:

{
  A string
  B string
  C string
  D int
  E string
}
Run Code Online (Sandbox Code Playgroud)

我不确定如何做到这一点,因为我已经进行了引用和取消引用,但似乎还没有成功。

string json escaping go

3
推荐指数
1
解决办法
5530
查看次数

Visual Studio代码侧边栏选择颜色

如何更改Visual Studio侧边栏文件的突出显示/选择颜色?

在此处输入图片说明

settings json visual-studio-code

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

解决goroutines僵局

我一直在努力解决我在Golang并发中遇到的这个简单问题.我一直在寻找所有可能的解决方案,但没有发现我的问题(或者我可能会错过一个).这是我的代码:

package main

import (
    "fmt"
    "time"
)

func producer(ch chan int, d time.Duration, num int) {

    for i:=0; i<num; i++ {
        ch <- i
        time.Sleep(d)
    }
}

func main() {
    ch := make(chan int)

    go producer(ch, 100*time.Millisecond, 2)
    go producer(ch, 200*time.Millisecond, 5)

    for {
        fmt.Println(<-ch)    
    }

    close(ch)
}
Run Code Online (Sandbox Code Playgroud)

它打印错误:

致命错误:所有goroutines都睡着了 - 僵局!

goroutine 1 [chan receive]:main.main()D:/Code/go/src/testconcurrency/main.go:23 + 0xca退出状态2

什么是避免此错误的有效方法?,谢谢.

concurrency channel go goroutine

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

Golang子切片

在 Go 中,为什么这段代码可以工作:

package main

import (
  "fmt"
)

func main() {
  a := []int{1}
  a = a[1:]
  fmt.Println(len(a))
}
Run Code Online (Sandbox Code Playgroud)

但这并没有:

package main

import (
  "fmt"
)

func main() {
  a := []int{1}
  a = a[2:]
  fmt.Println(len(a))
}
Run Code Online (Sandbox Code Playgroud)

我听说过切片容量,有人可以详细说明吗?

arrays go slice

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

重试 http 请求 RoundTrip

我做了一个服务器,客户端通过 http 进行访问。我在客户端的 Transport 的 RoundTripper 方法中设置了重试机制。以下是每个服务器和客户端的工作代码示例:

服务器主.go

package main

import (
    "fmt"
    "net/http"
    "time"
)

func test(w http.ResponseWriter, req *http.Request) {
    time.Sleep(2 * time.Second)
    fmt.Fprintf(w, "hello\n")
}

func main() {
    http.HandleFunc("/test", test)
    http.ListenAndServe(":8090", nil)
}

Run Code Online (Sandbox Code Playgroud)

客户端main.go

package main

import (
    "context"
    "fmt"
    "log"
    "net/http"
    "time"
)

type Retry struct {
    nums      int
    transport http.RoundTripper
}

// to retry
func (r *Retry) RoundTrip(req *http.Request) (resp *http.Response, err error) {
    for i := 0; i < r.nums; i++ {
        log.Println("Attempt: ", …
Run Code Online (Sandbox Code Playgroud)

client timeout http go server

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

Golang SQL包中的QueryRow和Exec有什么区别?

在 Golang SQL 包中有 QueryRow 和 Exec 用于执行查询。如果我在事务中执行插入查询,在性能方面使用哪个更好?

err = tx.QueryRow(query, params).Scan(&id)

对比

result, err = tx.Exec(query, params)

sql transactions go

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

在 Golang 中使用上下文超时停止正在运行的函数

我想利用 golang 中的上下文在超时时用于取消。

代码:

package main

import "fmt"
import "time"
import "context"

func F(ctx context.Context) error {
  ctx, cancel := context.WithTimeout(ctx,3*time.Second)
  defer cancel()
  for i:=0;i<10;i++ {
    time.Sleep(1 * time.Second)
    fmt.Println("No: ",i)
  }
  select {
    case <-ctx.Done():
      fmt.Println("TIME OUT")
      cancel()
      return ctx.Err()
    default:
      fmt.Println("ALL DONE")
      return nil
  }
}

func main() {
  ctx := context.Background()
  err := F(ctx)
  if err != nil {
    fmt.Println(err)
  }else {
    fmt.Println("Success")
  }
}
Run Code Online (Sandbox Code Playgroud)

期望:上面的代码应该在 counter 处停止运行循环2,因为超时为 3 秒,循环每次运行 1 秒。所以我期待这样的事情:

No:  0
No: …
Run Code Online (Sandbox Code Playgroud)

go goroutine

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

Golang 中的编组结构与映射

返回 JSON 结果时,使用 marshalledstructmap. 我见过一些代码使用 map 并将它们编组为 json 返回,其他代码使用编组结构返回 json 响应。

例子:

使用struct

type Response struct {
    A int `json:"a"`
    B string `json:"b"`
}

r := Response{A:1,B:"1"}
resp, _ := json.Marshal(r)
Run Code Online (Sandbox Code Playgroud)

使用map

m := map[string]interface{}{
    A: 1,
    B: "1",
}
resp, _ := json.Marshal(m)
Run Code Online (Sandbox Code Playgroud)

哪个更好?

json marshalling go

0
推荐指数
1
解决办法
1929
查看次数

访问Golang Web服务器

我知道 Golang 包含本机内置 Web 服务器(net/http),可以用作服务器而无需使用外部 Web 服务器(apache、nginx 等)。对于本地开发,您只需运行即可http.ListenAndServe,您的服务器已在本地准备就绪。

我的问题是,如何设置您的 golang 应用程序以供其他人公开访问,而不需要外部 Web 服务器?

webserver go

-4
推荐指数
1
解决办法
4218
查看次数