exporter

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package exporter provides sets of function to export variables to a GO code.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Export

func Export(i interface{}) (string, error)

Export exports input value to a GO code.

Example (EmptySlice)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	var v interface{} = make([]interface{}, 0)
	s, _ := exporter.Export(v)
	fmt.Println(s)
}
Output:

make([]interface{}, 0)
Example (EmptySlice2)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	var v []interface{} = nil
	s, _ := exporter.Export(v)
	fmt.Println(s)
}
Output:

make([]interface{}, 0)
Example (Err)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	_, err := exporter.Export(struct{}{})
	fmt.Println(err)
}
Output:

type `struct {}` is not supported
Example (Int)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	s, _ := exporter.Export(5)
	fmt.Println(s)
}
Output:

int(5)
Example (Pi)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	s, _ := exporter.Export(float32(3.1416))
	fmt.Println(s)
}
Output:

float32(3.1416)
Example (Slice)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	s, _ := exporter.Export([]uint{1, 2, 3})
	fmt.Println(s)
}
Output:

[]uint{uint(1), uint(2), uint(3)}
Example (Slice2)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	s, _ := exporter.Export([]interface{}{int32(1), int64(2), float32(3.14), "hello world"})
	fmt.Println(s)
}
Output:

[]interface{}{int32(1), int64(2), float32(3.14), "hello world"}
Example (String)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	s, _ := exporter.Export("hello world")
	fmt.Println(s)
}
Output:

"hello world"

func MustExport

func MustExport(i interface{}) string

MustExport exports input value to a GO code.

func MustToString

func MustToString(i interface{}) string

MustToString casts input value to a string. See ToString.

func ToString

func ToString(i interface{}) (string, error)

ToString casts input value to a string. This function supports booleans, strings, numeric values and nil-values:

  • any numeric input returns string that represents its value without a type
  • any boolean input returns accordingly a string "true" or "false"
  • any string input results in the output that equals the input
  • any nil input returns a "nil" string.
Example (Bool)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	s, _ := exporter.ToString(false)
	fmt.Println(s)
}
Output:

false
Example (Nil)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	s, _ := exporter.ToString(nil)
	fmt.Println(s)
}
Output:

nil
Example (NotSupported)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	_, err := exporter.ToString(struct{}{})
	fmt.Println(err)
}
Output:

type `struct {}` is not supported
Example (Pi)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	s, _ := exporter.ToString(float32(3.1416))
	fmt.Println(s)
}
Output:

3.1416
Example (String)
package main

import (
	"fmt"

	"github.com/gomponents/gontainer-helpers/exporter"
)

func main() {
	s, _ := exporter.ToString("hello world")
	fmt.Println(s)
}
Output:

hello world

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL