|
| 1 | +# Utilities |
| 2 | + |
| 3 | +GoWandBox also offers a couple of utilities, most of which wrap around the WandBox API - |
| 4 | + |
| 5 | +## List Supported Languages |
| 6 | + |
| 7 | +```go |
| 8 | +languages, err := gwb.GetLanguages(10000) // Timeout of 10000 milliseconds |
| 9 | + |
| 10 | +if err != nil { |
| 11 | + log.Fatal(err) |
| 12 | +} |
| 13 | + |
| 14 | +for _, lang := range languages() { |
| 15 | + fmt.Printf("Language - %v\n", lang.DisplayName) // Name of the language |
| 16 | + fmt.Println(lang.Name) // Name with version |
| 17 | + fmt.Printf("Version - %v\n", lang.Version) |
| 18 | + fmt.Printf("Used with - %v\n", lang.DisplayCompileCommand) // Command used to compile/run |
| 19 | + fmt.Printf( |
| 20 | + "Supports Compiler Options - %v\nSupports Runtime Options - %v\n", |
| 21 | + lang.CompilerOptionRaw, |
| 22 | + lang.RuntimeOptionRaw, |
| 23 | + ) |
| 24 | + fmt.Printf("Switches - %v\n", lang.Switches) // lang.Switches is an array of switches |
| 25 | + fmt.Printf("Template Names - %v\n", lang.Templates) |
| 26 | + // lang.Templates is an array of template names, |
| 27 | + // which can be obtained with the GetTemplate() function |
| 28 | + |
| 29 | + fmt.Println("--------------------------------") |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +``` |
| 34 | +$ go run main.go |
| 35 | +Language - gcc HEAD |
| 36 | +gcc-head-c |
| 37 | +Version - 12.0.0 20210721 (experimental) |
| 38 | +Used with - gcc prog.c |
| 39 | +Supports Compiler Options - true |
| 40 | +Supports Runtime Options - false |
| 41 | +Switches - [{true warning -Wall -Wextra Warnings} {false optimize -O2 -march=native Optimization} {false cpp-verbose -v Verbose} {gnu11 std-c } {cpp-no-pedantic cpp-pedantic }] |
| 42 | +Template Names - [gcc-c] |
| 43 | +-------------------------------- |
| 44 | +Language - gcc |
| 45 | +gcc-11.1.0-c |
| 46 | +Version - 11.1.0 |
| 47 | +Used with - gcc prog.c |
| 48 | +Supports Compiler Options - true |
| 49 | +Supports Runtime Options - false |
| 50 | +Switches - [{true warning -Wall -Wextra Warnings} {false optimize -O2 -march=native Optimization} {false cpp-verbose -v Verbose} {gnu11 std-c } {cpp-no-pedantic cpp-pedantic }] |
| 51 | +Template Names - [gcc-c] |
| 52 | +-------------------------------- |
| 53 | +...... |
| 54 | +``` |
| 55 | + |
| 56 | +## Get Template |
| 57 | + |
| 58 | +You can get the template for a language, with the `GetTemplate()` function. |
| 59 | + |
| 60 | +```go |
| 61 | +template, err := gwb.GetTemplate("cpython", 10000) |
| 62 | +// Get the template for `cpython`, with a 10000 millisecond timeout |
| 63 | + |
| 64 | +if err != nil { |
| 65 | + log.Fatal(err) |
| 66 | +} |
| 67 | + |
| 68 | +fmt.Println(template.Code) |
| 69 | +``` |
| 70 | + |
| 71 | +``` |
| 72 | +$ go run main.go |
| 73 | +
|
| 74 | +# This file is a "Hello, world!" in Python language by CPython for wandbox. |
| 75 | +
|
| 76 | +print("Hello, world!") |
| 77 | +
|
| 78 | +# CPython references: |
| 79 | +# https://www.python.org/ |
| 80 | +``` |
| 81 | + |
| 82 | +## Get User State |
| 83 | + |
| 84 | +You can view the state of a WandBox user, given their SessionKey (Provided by WandBox) - |
| 85 | +```go |
| 86 | +user, err := gwb.GetUser("session-key", 10000) |
| 87 | +if err != nil { |
| 88 | + log.Fatal(err) |
| 89 | +} |
| 90 | + |
| 91 | +fmt.Printf("Username: %v\n", user.Username) |
| 92 | +fmt.Printf("Logged in Currently: %v\n", user.Login) |
| 93 | +``` |
| 94 | + |
| 95 | +``` |
| 96 | +$ go run main.go |
| 97 | +Username: ABCDEFGH |
| 98 | +Logged in Currently: true |
| 99 | +``` |
| 100 | + |
| 101 | +## Get PermLinks |
| 102 | + |
| 103 | +If you have the the PermLink of a program that was executed, and saved, you can retrieve information about it, such as the code, input, compiler used, time it was executed at, output, errors, etc. |
| 104 | + |
| 105 | +```go |
| 106 | +result, err := GetPermLink("permlink", 10000) // Timeout of 10000 milliseconds |
| 107 | + |
| 108 | +if err != nil { |
| 109 | + log.Fatal(err) |
| 110 | +} |
| 111 | + |
| 112 | +fmt.Printf("Code: %v\n", result.Parameter.Code) |
| 113 | +fmt.Printf("Files: %v\n", result.Parameter.Codes) |
| 114 | +fmt.Printf("Compiled with: %v\n", result.Parameter.Compiler) |
| 115 | + |
| 116 | +fmt.Printf("Output: %v\n", result.Result.ProgramMessage) |
| 117 | +fmt.Printf("Exit Code: %v\n", result.Result.Status) |
| 118 | +``` |
| 119 | + |
| 120 | +``` |
| 121 | +$ go run main.go |
| 122 | +Code: print(123) |
| 123 | +Files: [] |
| 124 | +Compiled with: cpython-head |
| 125 | +Output: 123 |
| 126 | +
|
| 127 | +Exit Code: 0 |
| 128 | +``` |
| 129 | + |
| 130 | +For a detailed list of information available, check out the pkg.go.dev page - [https://pkg.go.dev/github.com/classPythonAddike/gowandbox](https://pkg.go.dev/github.com/classPythonAddike/gowandbox#GWBPermLink) |
| 131 | + |
| 132 | +## Change WandBox API URL |
| 133 | + |
| 134 | +You can use your own instance of WandBox, instead of the default one with `ChangeWandBoxUrl()` |
| 135 | + |
| 136 | +```go |
| 137 | +gwb.ChangeWandBoxUrl("https://my.wandboxinstance.com/api/") |
| 138 | +``` |
| 139 | + |
| 140 | +To reset the WandBox URL - |
| 141 | +```go |
| 142 | +gwb.ResetWandBoxUrl() |
| 143 | +``` |
0 commit comments