要转换为struct的字符串数组

时间:2016-09-23 20:26:57

标签: arrays json go

这是我执行的命令

    <?php
            $server = "\SQLEXPRESS";
        $options = array( "Database"=>"test" );


        $conn = sqlsrv_connect( $server, $options );


        if( !$conn ) die( print_r( sqlsrv_errors(), true ) );

$query = "select * from VW_VEHICLE_STATUS";
$sql = sqlsrv_query($conn, $query);


            echo "<table >";
            echo "<tr>";
            echo "<td style='border:1px solid black;Font-size=18;Font-weight=bold'>";
            echo "MAKE";
            echo "</td>";
            echo "<td style='border:1px solid black;Font-size=18;Font-Weight=bold'>";
            echo "MODEL";
            echo "</td>";
            echo "<td style='border:1px solid black;Font-size=18;Font-Weight=bold'>";
            echo "REGISTRATION";
            echo "</td>";
               echo "<td style='border:1px solid black;Font-size=18;Font-Weight=bold'>";
            echo "REPAIR_START_DATE";
            echo "</td>";
               echo "<td style='border:1px solid black;Font-size=18;Font-Weight=bold'>";
            echo "REPAIR_END_DATE";
            echo "</td>";
               echo "<td style='border:1px solid black;Font-size=18;Font-Weight=bold'>";
            echo "DESCRIPTION";
            echo "</td>";
            echo "</tr>\n";

           while ($row = sqlsrv_fetch_array($sql)) 
            {
                echo "<tr>";
                echo "<td style='border:1px solid black'>";
                echo $row['MAKE']; 
                echo "</td>";
                echo "<td style='border:1px solid black'>";
                echo $row['MODEL']; 
                echo "</td>";
                echo "<td style='border:1px solid black'>";
                echo $row['REGISTRATION']; 
                echo "</td>";
                     echo "<td style='border:1px solid black'>";
                echo $row['REPAIR_START_DATE']->format("Y-M-d"); 
                echo "</td>";
                     echo "<td style='border:1px solid black'>";
                echo $row['REPAIR_END_DATE']->format("Y-M-d"); ; 
                echo "</td>";
                   echo "<td style='border:1px solid black'>";
                echo $row['DESCRIPTION']; 
                echo "</td>";
                echo "</tr>\n";
            }
            echo "</table>";

            sqlsrv_close( $conn);
            ?>

我使用$ ps -e PID PPID PGID WINPID TTY UID STIME COMMAND 4372 1 4372 4372 ? 197608 03:44:57 /usr/bin/mintty 6476 4372 6476 6208 pty0 197608 03:44:58 /usr/bin/bash 14484 6476 14484 12888 pty0 197608 13:23:48 /usr/bin/ps scanner.scanLines获得1d字符串数组。我需要将其转换为结构数组:

bufio

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

strings.Fields包中有一个方便的strings函数,可以帮助解析这种输出。 Go喜欢实用的方法,所以天真的实现将是:

  • 遍历您的数组并将每一行拆分为以空格为分隔符的字段
  • 从这些字段构造新的ProcessInfo对象
  • 将此对象添加到数组

假设您的数组名为lines,请执行以下操作:

var pinfos []ProcessInfo
for _, line := range lines {
    fields := strings.Fields(line)

    pi := ProcessInfo{
        PID:     fields[0],
        PPID:    fields[1],
        PGID:    fields[2],
        WINPID:  fields[3],
        TTY:     fields[4],
        UID:     fields[5],
        STIME:   fields[6],
        COMMAND: fields[7],
    }

    pinfos = append(pinfos, pi)
}

请在此处查看完整代码: https://play.golang.org/p/wo8FFiYabA