Advertisement
cwchen

[Go] Overriding String method.

Oct 7th, 2017
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.40 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "strconv"
  6. )
  7.  
  8. type Vector []float64
  9.  
  10. func NewVector(args ...float64) Vector {
  11.     return args
  12. }
  13.  
  14. func (v Vector) String() string {
  15.     out := "("
  16.  
  17.     for i, e := range v {
  18.         out += strconv.FormatFloat(e, 'f', -2, 64)
  19.  
  20.         if i < len(v)-1 {
  21.             out += ", "
  22.         }
  23.     }
  24.  
  25.     out += ")"
  26.  
  27.     return out
  28. }
  29.  
  30. func main() {
  31.     v := NewVector(1, 2, 3, 4, 5)
  32.  
  33.     fmt.Println(v)
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement