Formatting code and log snippets

Including code and logs in your posts makes it easier for others to understand and reply.

Here are some tips for formatting your snippets:

  1. Include short snippets rather than long or complete code or logs.
  2. Use text rather than images. Text is searchable and others can copy and paste your code to easily debug and help you.
  3. Add triple backticks ``` before and after a snippet of code. The programming langauge will automatically be detected and highlighted, which is an improvement over copy & paste.
  4. Quote logs by prefixing them with > to visually distinguish them from code and wrap long lines.
  5. Add spacing to break up long blocks of text and improve readability.
  6. If you need to share lengthy snippets or complete projects please use a GitHub repo or GitHub gists.

Before & after: code fencing (```)

Bad:

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
// Set up the bucketed leaderboards tournament
id := “bucketed_weekly”
metadata := map[string]interface{}{}
if err := nk.TournamentCreate(ctx, id, “desc”, “incr”, “0 0 * * 0”, metadata,
“Bucketed Weekly #1”, “”, 1, 0, 0, 604800, 100000000, 10000000, false); err != nil {
return err
}
if err := initializer.RegisterRpc(“get_bucket_records”, rpcGetBucketRecordsFn(string{id}, 2000)); err != nil {
return err
}
return nil
}

Good:

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
    // Set up the bucketed leaderboards tournament
    id := "bucketed_weekly"
    metadata := map[string]interface{}{}

    if err := nk.TournamentCreate(ctx, id, "desc", "incr", "0 0 * * 0", metadata,
        "Bucketed Weekly #1", "", 1, 0, 0, 604800, 100000000, 10000000, false); err != nil {
        return err
    }
    if err := initializer.RegisterRpc("get_bucket_records", rpcGetBucketRecordsFn([]string{id}, 2000)); err != nil {
        return err
    }
    return nil
}

Before & after: quoting and line spacing

Bad:

{“level”:“fatal”,“ts”:“2019-12-19T09:40:22.153-0500”,“msg”:“Failed initializing runtime modules”,“error”:“plugin.Open(”/Users/Shared/Jenkins/nakama_server/data/modules/go_source/module.so"): dlopen(/Users/Shared/Jenkins/nakama_server/data/modules/go_source/module.so, 10): no suitable image found. Did find:\n\t/Users/Shared/Jenkins/nakama_server/data/modules/go_source/module.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00\n\t/Users/Shared/Jenkins/nakama_server/data/modules/go_source/module.so: stat() failed with errno=60",“stacktrace”:proc.go:203”}

Good:

{“level”:“fatal”,“ts”:“2019-12-19T09:40:22.153-0500”,“msg”:“Failed initializing runtime modules”,“error”:“plugin.Open(”/Users/Shared/Jenkins/nakama_server/data/modules/go_source/module.so"): dlopen(/Users/Shared/Jenkins/nakama_server/data/modules/go_source/module.so, 10): no suitable image found. Did find:\n\t/Users/Shared/Jenkins/nakama_server/data/modules/go_source/module.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00\n\t/Users/Shared/Jenkins/nakama_server/data/modules/go_source/module.so: stat() failed with errno=60",“stacktrace”:“proc.go:203”}