Skip to content

Commit

Permalink
Created using Colab
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed May 6, 2024
1 parent 554bd51 commit 72408c2
Showing 1 changed file with 241 additions and 1 deletion.
242 changes: 241 additions & 1 deletion Notebooks/1_go_unicode_char_literals.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1,241 @@
{"cells":[{"cell_type":"markdown","metadata":{"id":"FWzjioUI63tT"},"source":["# Unicode Character Literals"]},{"cell_type":"markdown","metadata":{"id":"i-bezriyHDdD"},"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":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":102512,"status":"ok","timestamp":1714945867744,"user":{"displayName":"Sualeh Fatehi","userId":"04758152281280757291"},"user_tz":240},"id":"3PaXSu67xkrg","outputId":"7d5ee853-d919-442a-c2f1-742e82717f3d"},"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":{},"source":["----------"]},{"cell_type":"markdown","metadata":{"id":"HadGU80XudVz"},"source":["## Valid Character Literals"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":481,"status":"ok","timestamp":1714946946017,"user":{"displayName":"Sualeh Fatehi","userId":"04758152281280757291"},"user_tz":240},"id":"FqH3tlqjsDpV","outputId":"41d6db36-9726-49db-f332-b8917290a625"},"outputs":[],"source":["%%\n","ch1 := 'a'\n","ch2 := '東' // (Not an ASCII character!)\n","ch3 := '𐐀' // (Not a BMP character!)\n","// ch4 := '\\' // (Backslash is a syntax error in Go!)\n","\n","fmt.Printf(\"%c %c %c\\n\", ch1, ch2, ch3)"]},{"cell_type":"markdown","metadata":{"id":"FXz5BuqczgzZ"},"source":["## Unicode Notation in Strings\n","\n","- `\\uHHHH` - where H is a case-insensitive hexadecimal character\n","- Only supports the Basic Multilingual Plane"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":616,"status":"ok","timestamp":1714947118452,"user":{"displayName":"Sualeh Fatehi","userId":"04758152281280757291"},"user_tz":240},"id":"aK16puWgzhsl","outputId":"7a560cec-d4b4-4977-f026-05c2e3657f3b"},"outputs":[],"source":["%%\n","ch5 := '\\u00EA' // ‘ê’\n","str1 := \"a\\u00ea\\u00f1\\u00fcc\" // “aêñüc”\n","str2 := \"A\\u00EA\\u00F1\\u00FCC\" // “AêñüC”\n","\n","fmt.Printf(\"%c\\n\", ch5)\n","fmt.Println(str1)\n","fmt.Println(str2)"]},{"cell_type":"markdown","metadata":{"id":"prsKwd-m2nT_"},"source":["## Unicode Code Point Literals\n","\n","- `\\U00HHHHHH` for specifying code point plane and code point for characters outside the BMP"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":315,"status":"ok","timestamp":1714947868441,"user":{"displayName":"Sualeh Fatehi","userId":"04758152281280757291"},"user_tz":240},"id":"lVkkNx5S2oqi","outputId":"febbed40-6623-4bfe-e6c4-4e9eba18ff6a"},"outputs":[],"source":["%%\n","str3 := \"\\U00010400\" // '𐐀'\n","\n","fmt.Println(str3)\n","fmt.Printf(\"length: %d\\n\", utf8.RuneCountInString(str3))"]},{"cell_type":"markdown","metadata":{"id":"CtIvqiZy3C50"},"source":["## Unicode Code Point Literals as Integers\n","\n","- `0xHHHHHH` for specifying code point plane and code point for characters outside the BMP\n","- Use the `chr()` function"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":136,"status":"ok","timestamp":1714951765615,"user":{"displayName":"Sualeh Fatehi","userId":"04758152281280757291"},"user_tz":240},"id":"mx9GXV4J3EtZ","outputId":"8e98b5e4-e949-494e-f656-1a58ff1ae0b3"},"outputs":[],"source":["%%\n","cp1 := '\\U00010400' // '𐐀'\n","str4 := string(cp1)\n","\n","fmt.Println(str4)\n","fmt.Printf(\"length: %d\\n\", utf8.RuneCountInString(str4))"]},{"cell_type":"markdown","metadata":{"id":"6nVgUBOTGmcY"},"source":["## Escape Sequences"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":657,"status":"ok","timestamp":1714952032909,"user":{"displayName":"Sualeh Fatehi","userId":"04758152281280757291"},"user_tz":240},"id":"KMGAl1njGoJE","outputId":"47be2164-bf77-4977-b215-8df2d084876b"},"outputs":[],"source":["func printUnicode(ch rune) {\n"," fmt.Printf(\"\\\\u%04x\\n\", ch)\n","}\n","\n","%%\n","printUnicode('\\t')\n","printUnicode('\\b')\n","printUnicode('\\n')\n","printUnicode('\\r')\n","printUnicode('\\f')\n","printUnicode('\\'')\n","printUnicode('\"')\n","printUnicode('\\\\')"]}],"metadata":{"colab":{"provenance":[{"file_id":"1vUd3SSoOm2K6UQLnkJQursZZx4CaIT_1","timestamp":1714945497637}]},"kernelspec":{"display_name":"Go (gonb)","name":"gonb"}},"nbformat":4,"nbformat_minor":0}
{
"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/1_go_unicode_char_literals.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FWzjioUI63tT"
},
"source": [
"# Unicode Character Literals"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "i-bezriyHDdD"
},
"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": {
"id": "3PaXSu67xkrg",
"cellView": "form"
},
"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": "8Qp_s5HtUBcA"
},
"source": [
"----------"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HadGU80XudVz"
},
"source": [
"## Valid Character Literals"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FqH3tlqjsDpV"
},
"outputs": [],
"source": [
"%%\n",
"ch1 := 'a'\n",
"ch2 := '東' // (Not an ASCII character!)\n",
"ch3 := '𐐀' // (Not a BMP character!)\n",
"// ch4 := '\\' // (Backslash is a syntax error in Go!)\n",
"\n",
"fmt.Printf(\"%c %c %c\\n\", ch1, ch2, ch3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FXz5BuqczgzZ"
},
"source": [
"## Unicode Notation in Strings\n",
"\n",
"- `\\uHHHH` - where H is a case-insensitive hexadecimal character\n",
"- Only supports the Basic Multilingual Plane"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "aK16puWgzhsl"
},
"outputs": [],
"source": [
"%%\n",
"ch5 := '\\u00EA' // ‘ê’\n",
"str1 := \"a\\u00ea\\u00f1\\u00fcc\" // “aêñüc”\n",
"str2 := \"A\\u00EA\\u00F1\\u00FCC\" // “AêñüC”\n",
"\n",
"fmt.Printf(\"%c\\n\", ch5)\n",
"fmt.Println(str1)\n",
"fmt.Println(str2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "prsKwd-m2nT_"
},
"source": [
"## Unicode Code Point Literals\n",
"\n",
"- `\\U00HHHHHH` for specifying code point plane and code point for characters outside the BMP"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "lVkkNx5S2oqi"
},
"outputs": [],
"source": [
"%%\n",
"str3 := \"\\U00010400\" // '𐐀'\n",
"\n",
"fmt.Println(str3)\n",
"fmt.Printf(\"length: %d\\n\", utf8.RuneCountInString(str3))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CtIvqiZy3C50"
},
"source": [
"## Unicode Code Point Literals as Integers\n",
"\n",
"- `0xHHHHHH` for specifying code point plane and code point for characters outside the BMP\n",
"- Use the `chr()` function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "mx9GXV4J3EtZ"
},
"outputs": [],
"source": [
"%%\n",
"cp1 := '\\U00010400' // '𐐀'\n",
"str4 := string(cp1)\n",
"\n",
"fmt.Println(str4)\n",
"fmt.Printf(\"length: %d\\n\", utf8.RuneCountInString(str4))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6nVgUBOTGmcY"
},
"source": [
"## Escape Sequences"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "KMGAl1njGoJE"
},
"outputs": [],
"source": [
"func printUnicode(ch rune) {\n",
" fmt.Printf(\"\\\\u%04x\\n\", ch)\n",
"}\n",
"\n",
"%%\n",
"printUnicode('\\t')\n",
"printUnicode('\\b')\n",
"printUnicode('\\n')\n",
"printUnicode('\\r')\n",
"printUnicode('\\f')\n",
"printUnicode('\\'')\n",
"printUnicode('\"')\n",
"printUnicode('\\\\')"
]
}
],
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Go (gonb)",
"name": "gonb"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 72408c2

Please sign in to comment.