|
| 1 | +# Compiling |
| 2 | + |
| 3 | +GoWandBox (which wraps around the WandBox API) provides two methods to compile your program - |
| 4 | + |
| 5 | +## With GWBProgram |
| 6 | + |
| 7 | +You can compile code by using the `GWBProgram.Execute()` method. |
| 8 | + |
| 9 | +Make sure you've imported `github.com/classPythonAddike/gowandbox` as `gwb` |
| 10 | + |
| 11 | +First initialise a new GWBProgram - |
| 12 | +```go |
| 13 | +prog := gwb.NewGWBProgram() |
| 14 | + |
| 15 | +prog.Code = ` |
| 16 | +import gwbutil |
| 17 | +gwbutil.say() |
| 18 | +gwbutil.say() |
| 19 | +` // Main program |
| 20 | + |
| 21 | +// Additional files to use |
| 22 | +prog.Codes = []gwb.Program{ |
| 23 | + { |
| 24 | + "gwbutil.py", |
| 25 | + "def say(): print(input())", |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +// Show warnings |
| 30 | +prog.Options = "warning" |
| 31 | +prog.Compiler = "cpython-3.8.0" // Use cpython 3.8 |
| 32 | +prog.Stdin = "123\n456" // Input for the program |
| 33 | +prog.SaveCode = true // Save it to a permlink |
| 34 | +``` |
| 35 | + |
| 36 | +Execute it with `.Execute()` |
| 37 | +```go |
| 38 | +result, err := prog.Execute(10000) // We want the request to timeout after 10,000 milliseconds |
| 39 | + |
| 40 | +if err != nil { |
| 41 | + log.Fatal(err) |
| 42 | + /* |
| 43 | + `err` is not nil if the response status code is not 200, |
| 44 | + if the request timed out, |
| 45 | + or an intnernal error ocurred in the wrapper |
| 46 | + */ |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +You can access information like errors, exit code and output from the `GWBResult` struct. |
| 51 | +```go |
| 52 | +fmt.Printf("Output during compile time - \"%v\"\n", result.CompilerOutput) |
| 53 | +fmt.Printf("Errors during compile time - \"%v\"\n", result.CompilerError) |
| 54 | + |
| 55 | +fmt.Printf("Output of program - \"%v\"\n", result.ProgramOutput) |
| 56 | +fmt.Printf("Errors during runtime - \"%v\"\n", result.ProgramError) |
| 57 | + |
| 58 | +fmt.Printf("Exit code - \"%v\"\n", result.Status) |
| 59 | +fmt.Printf("Signal - \"%v\"\n", result.Signal) |
| 60 | + |
| 61 | +fmt.Printf("View the program in your browser at \"%v\"\n", result.Url) |
| 62 | +fmt.Printf("Permlink to this program - \"%v\"\n", result.Permlink) |
| 63 | +``` |
| 64 | + |
| 65 | +``` |
| 66 | +$ go run main.go |
| 67 | +Output during compile time - "" |
| 68 | +Errors during compile time - "" |
| 69 | +Output of program - "123 |
| 70 | +456 |
| 71 | +" |
| 72 | +Errors during runtime - "" |
| 73 | +Exit code - "0" |
| 74 | +Signal - "" |
| 75 | +View the program in your browser at "https://wandbox.org/permlink/kOJgNvv8flGlApCA" |
| 76 | +Permlink to this program - "kOJgNvv8flGlApCA" |
| 77 | +``` |
| 78 | + |
| 79 | +## With GWBNDProgram |
| 80 | + |
| 81 | +WandBox offers an alternate method to compile your code - by using the `/compile.ndjson` endpoint. While this method is ideal for "streaming" the response, as it returns `NDJSON`, it is not as verbose as the former method. |
| 82 | + |
| 83 | +GoWandBox's API for GWBProgram and GWBNDProgram are very similar. |
| 84 | + |
| 85 | +First, declare your `GWBNDProgram` - |
| 86 | +```go |
| 87 | +prog := gwb.NewGWBNDProgram() |
| 88 | + |
| 89 | +prog.Code = ` |
| 90 | +import gwbutil, time |
| 91 | +gwbutil.say() |
| 92 | +time.sleep(5) |
| 93 | +gwbutil.say() |
| 94 | +` |
| 95 | +prog.Codes = []gwb.Program{ |
| 96 | + { |
| 97 | + "gwbutil.py", |
| 98 | + "def say(): print(input())", |
| 99 | + }, |
| 100 | +} |
| 101 | +prog.Options = "warning" |
| 102 | +prog.Compiler = "cpython-3.8.0" |
| 103 | +prog.Stdin = "123\n456" |
| 104 | +``` |
| 105 | + |
| 106 | +Then, execute your code - |
| 107 | +```go |
| 108 | +result, err := prog.Execute(10000) // Result is of the type `GWBNDReader` |
| 109 | + |
| 110 | +if err != nil { |
| 111 | + log.Fatal(err) |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +You can obtain the data with the `.Next()` method of the `GWBNDReader` - |
| 116 | +```go |
| 117 | +for { |
| 118 | + msg, err := result.Next() // msg is of the type GWBNDMessage |
| 119 | + |
| 120 | + if err == io.EOF { |
| 121 | + break |
| 122 | + } else if err == nil { |
| 123 | + fmt.Printf("Type - %v, Data - %v", msg.Type, msg.Data) |
| 124 | + } else { |
| 125 | + log.Fatal(err) |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +``` |
| 131 | +$ go run main.go |
| 132 | +Type - Control, Data - Start |
| 133 | +Type - StdOut, Data - 123 |
| 134 | +Type - StdOut, Data - 456 |
| 135 | +Type - ExitCode, Data - 0 |
| 136 | +Type - Control, Data - Finish |
| 137 | +``` |
0 commit comments