// Copyright 2013, 2014 Peter Vasil, Tomo Krajina. All // rights reserved. Use of this source code is governed // by a BSD-style license that can be found in the // LICENSE file. package gpx import ( "bytes" "encoding/xml" "errors" "io/ioutil" "os" "strings" "time" ) // An array cannot be constant :( The first one if the default layout: var TIMELAYOUTS = []string{ "2006-01-02T15:04:05Z", "2006-01-02T15:04:05", "2006-01-02 15:04:05Z", "2006-01-02 15:04:05", } func init() { /* fmt.Println("----------------------------------------------------------------------------------------------------") fmt.Println("This API is experimental, it *will* change") fmt.Println("----------------------------------------------------------------------------------------------------") */ } type ToXmlParams struct { Version string Indent bool } /* * Params are optional, you can set null to use GPXs Version and no indentation. */ func ToXml(g *GPX, params ToXmlParams) ([]byte, error) { version := g.Version if len(params.Version) > 0 { version = params.Version } indentation := params.Indent var gpxDoc interface{} if version == "1.0" { gpxDoc = convertToGpx10Models(g) } else if version == "1.1" { gpxDoc = convertToGpx11Models(g) } else { g.Version = "1.1" gpxDoc = convertToGpx11Models(g) } var buffer bytes.Buffer buffer.WriteString(xml.Header) if indentation { bytes, err := xml.MarshalIndent(gpxDoc, "", " ") if err != nil { return nil, err } buffer.Write(bytes) } else { bytes, err := xml.Marshal(gpxDoc) if err != nil { return nil, err } buffer.Write(bytes) } return buffer.Bytes(), nil } func guessGPXVersion(bytes []byte) (string, error) { bytesCount := 1000 if len(bytes) < 1000 { bytesCount = len(bytes) } startOfDocument := string(bytes[:bytesCount]) parts := strings.Split(startOfDocument, "