From 2a3e82376e7b6777fdb6482e6da827d44bee4fac Mon Sep 17 00:00:00 2001 From: croque15 Date: Wed, 16 Jul 2025 18:25:37 +0000 Subject: [PATCH] Refactor Go notebook: update execution counts and add new code cells demonstrating various Go concepts --- start.ipynb | 389 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 388 insertions(+), 1 deletion(-) diff --git a/start.ipynb b/start.ipynb index 96db506..bcdb9da 100644 --- a/start.ipynb +++ b/start.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 7, + "execution_count": 1, "id": "7ca8f628", "metadata": {}, "outputs": [ @@ -21,6 +21,393 @@ " fmt.Println(\"hello world\")\n", "}" ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "c49579da", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My favorite number is 3\n" + ] + } + ], + "source": [ + "package main\n", + "\n", + "import (\n", + "\t\"fmt\"\n", + "\t\"math/rand\"\n", + ")\n", + "\n", + "func main() {\n", + "\tfmt.Println(\"My favorite number is \", rand.Intn(10))\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "05b80284", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Now you have 2.6457513110645907 problems. \n" + ] + } + ], + "source": [ + "// Every Go program is made up of packages. Programs start running in package main.\n", + "\n", + "\n", + "package main\n", + "\n", + "import (\n", + "\t\"fmt\"\n", + "\t\"math\"\n", + ")\n", + "\n", + "func main() {\n", + "\tfmt.Printf(\"Now you have %g problems. \\n\", math.Sqrt(7))\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "99719199", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + } + ], + "source": [ + "// Exported names start with a capital letter\n", + "// Unexported names start with a lowercase letter\n", + "// When importing a package, you can reger only to its exported names. Any unexported names are no accessible from outside the package\n", + "\n", + "package main\n", + "\n", + "import (\n", + "\t\"fmt\"\n", + "\t\"math\"\n", + ")\n", + "\n", + "func main() {\n", + "\tfmt.Println(math.Pi)\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93070653", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n" + ] + } + ], + "source": [ + "// When two or more consecutive named function parameters share a type, you can omit the type from all but the last\n", + "\n", + "package main\n", + "\n", + "import \"fmt\"\n", + "\n", + "func add(x int, y int) int {\n", + "\treturn x + y\n", + "}\n", + "\n", + "func main() {\n", + "\tfmt.Println(add(42, 13))\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dee45842", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "world hello\n" + ] + } + ], + "source": [ + "// The swap function takes two strings and returns them in swapped order.\n", + "\n", + "package main\n", + "\n", + "import \"fmt\"\n", + "\n", + "func swap(x, y string) (string, string) {\n", + "\treturn y, x\n", + "}\n", + "\n", + "func main() {\n", + "\ta, b := swap(\"hello\", \"world\")\n", + "\tfmt.Println(a, b)\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24d2b265", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7 10\n" + ] + } + ], + "source": [ + "// Named return values\n", + "// A return statement without arguments returns the named return values This is known as \"naked\" return\n", + "\n", + "package main\n", + "\n", + "import \"fmt\"\n", + "\n", + "func split(sum int) (x, y int) {\n", + "\tx = sum * 4 / 9\n", + "\ty = sum - x\n", + "\treturn // naked return\n", + "}\n", + "\n", + "func main()\t{\n", + "\tfmt.Println(split(17))\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d78586f1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 false false false\n" + ] + } + ], + "source": [ + "// Variables declared without a corresponding initialization are given their zero value.\n", + "// The zero value is:\n", + "// 0 for numeric types, false for the boolean type, and \"\" (the empty string\n", + "\n", + "package main\n", + "\n", + "import \"fmt\"\n", + "\n", + "var c, python, java bool\n", + "\n", + "func main() {\n", + "\tvar i int\n", + "\tfmt.Println(i, c, python, java)\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "89256424", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 true false no!\n" + ] + } + ], + "source": [ + "// This code snippet demonstrates variable declaration and initialization in Go.\n", + "package main\n", + "\n", + "import \"fmt\"\n", + "\n", + "var i, j int = 1, 2\n", + "\n", + "func main() {\n", + "\tvar c, python, java = true, false, \"no!\"\n", + "\tfmt.Println(i, j, c, python, java)\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d6c0815f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 true false no!\n" + ] + } + ], + "source": [ + "// This is a complete Go program that is showing short variable delcaration and initialization.\n", + "// It declares variables i, j, and k with initial values, and c, python, and java with boolean and string values.\n", + "// := short assignment statement that is used in place of a var declaration with implicit type.\n", + "\n", + "package main\n", + "\n", + "import \"fmt\"\n", + "\n", + "func main() {\n", + "\tvar i, j int = 1,2\n", + "\tk := 3\n", + "\tc, python, java := true, false, \"no!\"\n", + "\tfmt.Println(i, j, k, c, python, java)\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8136a994", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Type: bool Value: false\n", + "Type: uint64 Value: 18446744073709551615\n", + "Type: complex128 Value: (2+3i)\n" + ] + } + ], + "source": [ + "// Basic types in Go include:\n", + "// bool, string, int, uint, int8, int16, int32, int64, uint8, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128\n", + "// The int and uint types are platform-dependent, meaning they can vary in size depending on the architecture (32-bit or 64-bit).\n", + "// The example shows variables of several types, and also that variable declarations may be \"factored\" into blocks, as with import statements.\n", + "\n", + "package main\n", + "\n", + "import (\n", + "\t\"fmt\"\n", + "\t\"math/cmplx\"\n", + ")\n", + "\n", + "var (\n", + "\tToBe bool \t = false\n", + "\tMaxInt uint64 = 1<<64 - 1\n", + "\tz complex128 = cmplx.Sqrt(-5 + 12i)\n", + ")\n", + "\n", + "func main() {\n", + "\tfmt.Printf(\"Type: %T Value: %v\\n\", ToBe, ToBe)\n", + "\tfmt.Printf(\"Type: %T Value: %v\\n\", MaxInt, MaxInt)\n", + "\tfmt.Printf(\"Type: %T Value: %v\\n\", z, z)\n", + "}\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ea95861f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0 false \"\"\n" + ] + } + ], + "source": [ + "// Zero values: 0 for numeric types, false for boolean type, and \"\" (the empty string) for strings\n", + "// Variables declared without an explicit initial value are given their zero value.\n", + "\n", + "package main\n", + "\n", + "import \"fmt\"\n", + "\n", + "func main() {\n", + "\tvar i int\n", + "\tvar f float64\n", + "\tvar b bool\n", + "\tvar s string\n", + "\tfmt.Printf(\"%v %v %v %q\\n\", i, f, b, s)\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "c7a9caf3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 4 5\n" + ] + } + ], + "source": [ + "// Type conversions: The expression T(v) converts the value v to the type T.\n", + "\n", + "package main\n", + "\n", + "import (\n", + "\t\"fmt\"\n", + "\t\"math\"\n", + ")\n", + "\n", + "func main() {\n", + "\tvar x, y int = 3, 4\n", + "\tvar f float64 = math.Sqrt(float64(x*x + y*y))\n", + "\tvar z uint = uint(f)\n", + "\tfmt.Println(x, y, z)\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8855aa39", + "metadata": {}, + "outputs": [], + "source": [ + "// Type interface " + ] } ], "metadata": {