如何在 golang 中写入 csv 时删除 <feff> BOM

how to remove <feff> BOM while writing csv in golang

我正在为我的 golang 服务器中的传入直径流量在 csv 文件中写入统计信息,但该文件在 rach 行的开头包含一个“”字符。

01.;34642222231118599998;21;6588283272|6588283272|300|0|46692|1582611861|,|2001|01.;34642222231118599998;21;65882=83=2272]|[|0 |1|1582611861 ****01.;34642222231118599998;22;6588080153|6588080153|300|0|46692|1582611861|,|2001|01.;34642222231118599998;22;6588080153|[=234=]|[=234=] 1|1582611861 ****01.;34642222231118599998;23;6587508893|6587508893|300|0|46692|1582611861|,|2001|01.;34642222231118599998;23;6587508893|[=234=]|[=234=] 1|1582611861

请指导我如何解决这个问题。

stats.go>>

     package main

     import (
       "encoding/csv"
       "os"
     )

   var (
        filename = "stats.csv"
     )

    // write the request and response to csv file
     func updateCSVFile(req string, resp string) error {
      file, err := os.OpenFile("/home/gyuser/charging-control-engine/stats/stats.csv",    os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
    if err != nil {
            return err
      }
    defer file.Close()

    // solve the encoding problem
    file.WriteString("\xEF\xBB\xBF")
    writer := csv.NewWriter(file)
    defer writer.Flush()
    return writer.Write([]string{req, resp})
     }

ccr.go >>>

       package main

    import (
      "log"
      "time"
      "github.com/fiorix/go-diameter/v4/diam/sm"
      "github.com/fiorix/go-diameter/v4/diam"
      "github.com/fiorix/go-diameter/v4/diam/avp"
      )

   const (
    // CCRInitial - ccr request type
    CCRInitial = 1
    // CCRUpdate - ccr request type
    CCRUpdate = 2
    // CCRTermination - ccr request type
    CCRTermination = 3
    // VM - flags VM-
    VM = avp.Mbit | avp.Vbit
    // M - flags M-
    M = avp.Mbit
    // TGPP - vendor id for TGPP
    TGPP = 10415
   )



  // Decoder for command requests
   type Decoder interface {
    Decode() (*diam.Message, error)
    WriteCSV() error
   }


  // CCRGxDecoder try to parse the gx requests with ccr
   type CCRGxDecoder struct {
    msg   *diam.Message
    gx    *GxStruct
    r     *CCRGxRequest
    q     *CCRGxResponse
    auth  bool
    start time.Time
    }


   // handle the Credit Control Request(CCR)
    func handleCCR(s *sm.Settings, conf *Config) diam.HandlerFunc {
    return func(c diam.Conn, m *diam.Message) {
            var decoder Decoder
            // application id for Gx and Gy
            //if m.Header.ApplicationID == conf.Gx.ID {
            //      decoder = NewCCRGxDecoder(m, &conf.Gx, conf.Server.Auth)
            //} else 
            if m.Header.ApplicationID == conf.Gy.ID {
                    decoder = NewCCRGyDecoder(m, &conf.Gy, conf.Server.Auth)
            } else {
                    log.Printf("invalid application id: %v\n", m.Header.ApplicationID)
                    return
            }

            // decode the requests and make the answer message
            nm, err := decoder.Decode()
            if err != nil {
                    log.Printf("decode failed: %v, %v\n", err, m)
                    return
            }

            // write the message back to the peer
            if _, err := nm.WriteTo(c); err != nil {
                    log.Printf("failed to write message: %v\n", err)
                    return
            }

            // update the request and response to csv file
            if err := decoder.WriteCSV(); err != nil {
                    log.Printf("write csv: %v\n", err)
            }


        }
     }
file.WriteString("\xEF\xBB\xBF")

只需从您的代码中删除这一行。这是UTF-8编码的BOM,和你看到的<feff>一模一样