Code Monkey home page Code Monkey logo

Comments (6)

MichaelS11 avatar MichaelS11 commented on August 23, 2024

I think there are other ways to solve this issue. For example, first run in the script with all the functions, then can run a script with just the wanted function. Could also look into using modules.

from anko.

vulpivia avatar vulpivia commented on August 23, 2024

I'm not sure I understand. I can run a whole script, but how would I then run the wanted function only?

Modules look basically like classes to me. Unless I can interact with them from outside Anko, I'm not sure that modules would help me.

from anko.

MichaelS11 avatar MichaelS11 commented on August 23, 2024

Split the script into two part. First run in the part with all the functions. Then run a script with just the function you want to call.

from anko.

vulpivia avatar vulpivia commented on August 23, 2024

Are the variables shared between the two parts?

Basically I have a function (or in this case script) that gets called in regular intervals and with each execution of the script, all variables are reset. My thought was that if I just call a function and not the whole script, I can have variables outside of that function that don't reset between calls.

from anko.

MichaelS11 avatar MichaelS11 commented on August 23, 2024

Any script can be run in any env, the env is where the script runs. Can keep running scripts as long as you want in the same env.

package main

import (
	"fmt"
	"log"

	"github.com/mattn/anko/env"
	"github.com/mattn/anko/vm"
)

func main() {
	e := env.NewEnv()

	err := e.Define("println", fmt.Println)
	if err != nil {
		log.Fatalf("Define error: %v\n", err)
	}

	script := `
a = 1
	`

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

	script = `
a = a + 1
println(a)
`

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

}

Output:

2
3
4

from anko.

vulpivia avatar vulpivia commented on August 23, 2024

Okay, that solves this issue. Thanks!

from anko.

Related Issues (20)

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.