doulu4413 2019-07-23 00:15
浏览 217
已采纳

如何返回整数字节数组中的最后一个元素

In Go, I want to find the last element in an array of integers.

I have a list:

    [0.0.1, 0.0.2, 0.0.3]

I just want:

    0.0.3

Every time I try to return the last element the console returns

    %!(EXTRA uint8=10)

Which I assume means I need to convert a byte array to a slice?

Here's my code:

cmd := exec.Command("git", "tag")
out, err := cmd.CombinedOutput()
if err != nil {
    log.Fatalf("cmd.Run() failed with %s
", err)
}
fmt.Printf("Variable Type:
%s
", reflect.TypeOf(out))
fmt.Printf("Variable:
%s
", (out))

slice := out[0: len(out)]
releaseVersion := slice[len(slice) - 1]
fmt.Printf("release version:", releaseVersion)

Here's the output:

Variable Type:
[]uint8

Variable:
0.0.1
0.0.2
0.0.3

release version:%!(EXTRA uint8=10)
  • 写回答

2条回答 默认 最新

  • dongwei9771 2019-07-23 01:37
    关注

    The reason you see: %!(EXTRA uint8=10) is you are calling Printf without a format option. You should just call fmt.Println instead.

    10 is the encoding of the newline character.

    Now, the meat of your question:

    You have a byte array (slice).

    You are trying to interpret it as lines (strings separated by ' ').

    Interpreting a byte array as a string is easy (assuming the bytes do in fact represent a utf-8 string):

    // assume buf is []byte
    var text = string(buf)
    

    You want to treat this as a list of strings. Since git tag returns lines, you can split by ' ' or you can just call strings.Fields which splits by generic whitespace (including newlines); documented here: https://golang.org/pkg/strings/#Fields

    var parts = strings.Fields(text)
    

    Now you can easily just get the last element:

    parts[len(parts)-1]
    

    But for safety you should check the list has some elements, otherwise you will crash on out-of-bounds access.

    cmd := exec.Command("git", "tag")
    bufOut, err := cmd.Output()
    if err != nil {
        log.Fatalf("cmd.Run() failed with %s
    ", err)
    }
    textOut := string(bufOut)
    parts := strings.Fields(textOut)
    if len(parts) == 0 {
        log.Fatal("No tags")
    }
    releaseVersion := parts[len(parts)-1]
    fmt.Println("release version:", releaseVersion)
    

    WHY DOES THIS GOT TO BE SO COMPLEX? IN JAVASCRIPT I CAN JUST DO pop()

    It's not really that complex. This is just how computers work. When a program runs, it has a standard output which is essentially a byte stream.

    I'm guessing javascript does this step for you automatically:

    textOut := string(bufOut)
    parts := strings.Fields(textOut)
    

    i.e. converting the program output to a list of strings separated by new lines.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错