Skip to content

Commit

Permalink
Merge pull request #19 from sualeh/go
Browse files Browse the repository at this point in the history
Add Go examples
  • Loading branch information
sualeh authored May 7, 2024
2 parents 638ae0f + c20b4f5 commit bd987b7
Show file tree
Hide file tree
Showing 4 changed files with 573 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Notebooks/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"recommendations": [
"bierner.markdown-footnotes",
"EditorConfig.EditorConfig",
"Katsute.settings-repository",
"ms-python.black-formatter",
"ms-python.pylint",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-toolsai.jupyter",
"ms-toolsai.jupyter-keymap",
"ms-toolsai.jupyter-renderers",
"ms-toolsai.vscode-jupyter-cell-tags",
"ms-toolsai.vscode-jupyter-slideshow",
"zeshuaro.vscode-python-poetry"
],
"unwantedRecommendations": []
}
38 changes: 38 additions & 0 deletions Notebooks/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"workbench.colorTheme": "Default Light Modern",
"[markdown]": {
"files.trimTrailingWhitespace": false,
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
}
},
"editor.foldingImportsByDefault": true,
"editor.lineHeight": 24,
"editor.minimap.enabled": false,
"editor.overviewRulerBorder": false,
"editor.tabSize": 2,
"editor.wrappingIndent": "indent",
"files.defaultLanguage": "markdown",
"files.exclude": {
"**/node_modules": true
},
"files.trimTrailingWhitespace": true,
"git.autofetch": true,
"git.enableCommitSigning": true,
"git.enableSmartCommit": true,
"git.postCommitCommand": "sync",
"git.showActionButton": {
"commit": false,
"publish": false,
"sync": false
},
"markdown.preview.breaks": true,
"workbench.editor.enablePreview": false,
"workbench.editorAssociations": {
"*.md": "vscode.markdown.preview.editor"
},
"scm.showActionButton": false,
"scm.showInputActionButton": false
}
274 changes: 274 additions & 0 deletions Notebooks/4_go_encoding.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/sualeh/What-a-Character/blob/go/Notebooks/4_go_encoding.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "69w_oXpjTlai"
},
"source": [
"----------\n",
"\n",
"## Google Colab\n",
"\n",
"You can run this notebook in Google Colab. The cell below should be run only once, and then followed by a change of runtime to `Go (gonb)`. Refresh the browser before running any subsequent code. If you are not running the notebook in Google Colab, skip this section."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "polyglot-notebook"
},
"id": "pL7TqK5kTlaj"
},
"outputs": [],
"source": [
"#@title Prepare Google Colab for Go Kernel\n",
"\n",
"# Install Go and goimports.\n",
"!echo -n \"Installing go ...\"\n",
"!mkdir -p cache\n",
"!wget -q -O cache/go.tar.gz 'https://go.dev/dl/go1.22.2.linux-amd64.tar.gz'\n",
"!tar xzf cache/go.tar.gz\n",
"%env GOROOT=/content/go\n",
"!ln -sf \"/content/go/bin/go\" /usr/bin/go\n",
"!echo \" done.\"\n",
"!go version\n",
"\n",
"# Install gonb, goimports, gopls.\n",
"!echo -n \"Installing gonb ...\"\n",
"!go install github.com/janpfeifer/gonb@latest >& /tmp/output || cat /tmp/output\n",
"!echo \" done.\"\n",
"!ln -sf /root/go/bin/gonb /usr/bin/gonb\n",
"\n",
"!echo -n \"Installing goimports ...\"\n",
"!go install golang.org/x/tools/cmd/goimports@latest >& /tmp/output || cat /tmp/output\n",
"!echo \" done.\"\n",
"!ln -sf /root/go/bin/goimports /usr/bin/goimports\n",
"\n",
"!echo -n \"Installing gopls ...\"\n",
"!go install golang.org/x/tools/gopls@latest >& /tmp/output || cat /tmp/output\n",
"!echo \" done.\"\n",
"!ln -sf /root/go/bin/gopls /usr/bin/gopls\n",
"\n",
"# Install gonb kernel configuration.\n",
"!gonb --install --logtostderr\n",
"!echo \"Done!\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ujliF5NSTlak"
},
"source": [
"----------"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "y5-0qPlZRNRv"
},
"source": [
"# Encoding"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EOz9sGI2RNRw"
},
"source": [
"## Converting to Bytes\n",
"\n",
"Always specify encoding to avoid cross-platform surprises."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "yNU6pAmXRNRx",
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"%%\n",
"original := \"Aß東𐐀\"\n",
"\n",
"utf16Bytes := utf16.Encode([]rune(original))\n",
"\n",
"// Convert UTF-16 bytes back to string\n",
"roundTrip := string(utf16.Decode(utf16Bytes))\n",
"\n",
"fmt.Println(roundTrip)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kElPJp6oRNRy"
},
"source": [
"> **Bad decoding**\n",
"\n",
"If an incorrect encoding is speccified, no exceptions may be thrown even if data gets corrupted."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6uMstf9XRNRy",
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"%%\n",
"original := \"Aß東𐐀\"\n",
"\n",
"utf16Bytes := utf16.Encode([]rune(original))\n",
"\n",
"// NOTE: Read the bytes back as UTF-8.\n",
"roundTrip := string(utf16Bytes)\n",
"\n",
"fmt.Println(roundTrip)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2AtIUt7TRNRy"
},
"source": [
"## Writing Files"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "F5IkwmzRRNRy",
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"%%\n",
"str := \"Aß東𐐀\"\n",
"\n",
"err := ioutil.WriteFile(\"test.txt\", []byte(str), 0644)\n",
"if err != nil {\n",
" panic(err)\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3bLKcVTgRNRy"
},
"source": [
"## Reading Files"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YfuallDiRNRy",
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"%%\n",
"content, err := ioutil.ReadFile(\"test.txt\")\n",
"if err != nil {\n",
" panic(err)\n",
"}\n",
"text := string(content)\n",
"\n",
"fmt.Println(text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "P3xW3MjYRNRz"
},
"source": [
"If you specify an incorrect encoding when reading a file, you can get gibberish."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "d5y1y6cbRNRz",
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"%%\n",
"file, err := os.Open(\"test.txt\")\n",
"if err != nil {\n",
" panic(err)\n",
"}\n",
"defer file.Close()\n",
"\n",
"reader := transform.NewReader(file, unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder())\n",
"content, err := ioutil.ReadAll(reader)\n",
"if err != nil {\n",
" panic(err)\n",
"}\n",
"text := string(content)\n",
"\n",
"fmt.Println(text)"
]
}
],
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Go (gonb)",
"name": "gonb"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit bd987b7

Please sign in to comment.