Giter Club home page Giter Club logo

icecream-go's Introduction

IceCream-Go

A Go port of Python's IceCream.

Usage

package main

import (
	"fmt"
	. "github.com/WAY29/icecream-go/icecream"
)
func foo(a int) int {
	return a + 333
}


func bar() {
	Ic()
}

func main() {
Ic(foo(123));
// Outputs:
// ic| foo(123): 456

Ic(1 + 5);
// Outputs:
// ic| 1 + 5: 6

Ic(foo(123), 1 + 5);
// Outputs:
// ic| foo(123): 456, 1 + 5: 6
bar();
}
// Outputs:
// ic| main.go:12 in main.bar()

Depends

Installation

go get -u "github.com/WAY29/icecream-go/icecream"

Configuration

If you want to change the prefix of the output, you can call icecream.ConfigurePrefix("Hello| ") (by default the prefix is ic| ).

If you want to change how the result is outputted, you can call icecream.ConfigureOutputFunction(f). func may be type of func(s interface{}). For example, if you want to log your messages to a file:

func logfile(s string) {
	filePath := "log.log"
	file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		fmt.Println(err)
	}
	defer file.Close()
	write := bufio.NewWriter(file)
	write.WriteString(s)
	write.Flush()
}
func main() {
	ic.ConfigureOutputFunction(logfile)
	ic.Ic(1, 2, 3)
}

If you want to change how the value is outputted, you can call icecream.ConfigureArgToStringFunction(f), func may be type of func(v interface{}) interface{}. For example, if you want to print more detail about a string:

func toString(v interface{}) interface{} {
	rv := reflect.ValueOf(v)
	if rv.Kind() == reflect.String {
		return fmt.Sprintf("[!string %#v with length %d!]", v, len(v.(string)))
	}
	return fmt.Sprintf("%#v", v)
}
func main() {
    s := "string"
	ic.ConfigureArgToStringFunction(toString)
    ic.Ic(s)
    ic.Ic("test")
}

If you want to add call's filename, line number, and parent function to ic's output, you can call icecream.ConfigureIncludeContext(true).

func main() {
    ic.ConfigureIncludeContext(true)
    ic.Ic(1, "asd")
}

If you want to reset configuration, you can call icecream.ResetPrefix(),icecream.ResetOutputFunction(), icecream.ResetArgToStringFunction(),icecream.ResetIncludeContext() .

icecream-go's People

Contributors

way29 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.