Initial commit - learning the basics
This commit is contained in:
		
						commit
						194ca9b7a9
					
				
							
								
								
									
										33
									
								
								api_gin/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								api_gin/go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
			
		||||
module learning.local/api_gin
 | 
			
		||||
 | 
			
		||||
go 1.24.5
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/bytedance/sonic v1.13.3 // indirect
 | 
			
		||||
	github.com/bytedance/sonic/loader v0.3.0 // indirect
 | 
			
		||||
	github.com/cloudwego/base64x v0.1.5 // indirect
 | 
			
		||||
	github.com/cloudwego/iasm v0.2.0 // indirect
 | 
			
		||||
	github.com/gabriel-vasile/mimetype v1.4.9 // indirect
 | 
			
		||||
	github.com/gin-contrib/sse v1.1.0 // indirect
 | 
			
		||||
	github.com/gin-gonic/gin v1.10.1 // indirect
 | 
			
		||||
	github.com/go-playground/locales v0.14.1 // indirect
 | 
			
		||||
	github.com/go-playground/universal-translator v0.18.1 // indirect
 | 
			
		||||
	github.com/go-playground/validator/v10 v10.27.0 // indirect
 | 
			
		||||
	github.com/goccy/go-json v0.10.5 // indirect
 | 
			
		||||
	github.com/json-iterator/go v1.1.12 // indirect
 | 
			
		||||
	github.com/klauspost/cpuid/v2 v2.3.0 // indirect
 | 
			
		||||
	github.com/leodido/go-urn v1.4.0 // indirect
 | 
			
		||||
	github.com/mattn/go-isatty v0.0.20 // indirect
 | 
			
		||||
	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 | 
			
		||||
	github.com/modern-go/reflect2 v1.0.2 // indirect
 | 
			
		||||
	github.com/pelletier/go-toml/v2 v2.2.4 // indirect
 | 
			
		||||
	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 | 
			
		||||
	github.com/ugorji/go/codec v1.3.0 // indirect
 | 
			
		||||
	golang.org/x/arch v0.19.0 // indirect
 | 
			
		||||
	golang.org/x/crypto v0.40.0 // indirect
 | 
			
		||||
	golang.org/x/net v0.42.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.34.0 // indirect
 | 
			
		||||
	golang.org/x/text v0.27.0 // indirect
 | 
			
		||||
	google.golang.org/protobuf v1.36.6 // indirect
 | 
			
		||||
	gopkg.in/yaml.v3 v3.0.1 // indirect
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										75
									
								
								api_gin/go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								api_gin/go.sum
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,75 @@
 | 
			
		||||
github.com/bytedance/sonic v1.13.3 h1:MS8gmaH16Gtirygw7jV91pDCN33NyMrPbN7qiYhEsF0=
 | 
			
		||||
github.com/bytedance/sonic v1.13.3/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
 | 
			
		||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
 | 
			
		||||
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
 | 
			
		||||
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
 | 
			
		||||
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
 | 
			
		||||
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
 | 
			
		||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
 | 
			
		||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
 | 
			
		||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
			
		||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
			
		||||
github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBvAG7U/oYY=
 | 
			
		||||
github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok=
 | 
			
		||||
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
 | 
			
		||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
 | 
			
		||||
github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ=
 | 
			
		||||
github.com/gin-gonic/gin v1.10.1/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
 | 
			
		||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
 | 
			
		||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
 | 
			
		||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
 | 
			
		||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
 | 
			
		||||
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
 | 
			
		||||
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
 | 
			
		||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
 | 
			
		||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
 | 
			
		||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
 | 
			
		||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
 | 
			
		||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
 | 
			
		||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
 | 
			
		||||
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
 | 
			
		||||
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
 | 
			
		||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
 | 
			
		||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
 | 
			
		||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
 | 
			
		||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 | 
			
		||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
 | 
			
		||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 | 
			
		||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
 | 
			
		||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
 | 
			
		||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
 | 
			
		||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
 | 
			
		||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
			
		||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 | 
			
		||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
 | 
			
		||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
 | 
			
		||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 | 
			
		||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 | 
			
		||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 | 
			
		||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
 | 
			
		||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
 | 
			
		||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
 | 
			
		||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
 | 
			
		||||
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
 | 
			
		||||
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
 | 
			
		||||
golang.org/x/arch v0.19.0 h1:LmbDQUodHThXE+htjrnmVD73M//D9GTH6wFZjyDkjyU=
 | 
			
		||||
golang.org/x/arch v0.19.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
 | 
			
		||||
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
 | 
			
		||||
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
 | 
			
		||||
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
 | 
			
		||||
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
 | 
			
		||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
 | 
			
		||||
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
 | 
			
		||||
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
 | 
			
		||||
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
 | 
			
		||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
 | 
			
		||||
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
 | 
			
		||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
			
		||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
 | 
			
		||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
 | 
			
		||||
							
								
								
									
										42
									
								
								api_gin/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								api_gin/main.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,42 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	router := gin.Default()
 | 
			
		||||
	router.GET("/", func(c *gin.Context) {
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{
 | 
			
		||||
			"message": "Hiya!",
 | 
			
		||||
		})
 | 
			
		||||
	})
 | 
			
		||||
	router.GET("/ping", func(c *gin.Context) {
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{
 | 
			
		||||
			"message": "pong",
 | 
			
		||||
		})
 | 
			
		||||
	})
 | 
			
		||||
	router.GET("/pokemon/:name", func(c *gin.Context) {
 | 
			
		||||
		name := c.Param("name")
 | 
			
		||||
		url := fmt.Sprintf("https://pokeapi.co/api/v2/pokemon/%s", name)
 | 
			
		||||
		res, err := http.Get(url)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Printf("Could not retrieve info for Pokemon %s!", name)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		var result map[string]any
 | 
			
		||||
		decoder := json.NewDecoder(res.Body)
 | 
			
		||||
		defer res.Body.Close()
 | 
			
		||||
		if err := decoder.Decode(&result); err != nil {
 | 
			
		||||
			fmt.Printf("Could not read response body!")
 | 
			
		||||
		}
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{
 | 
			
		||||
			"message": "success",
 | 
			
		||||
			"pokemon_info": result, 
 | 
			
		||||
		})
 | 
			
		||||
	})
 | 
			
		||||
	router.Run()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								hello/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								hello/go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
module learning.local/hello
 | 
			
		||||
 | 
			
		||||
go 1.24.5
 | 
			
		||||
 | 
			
		||||
replace learning.local/using_modules => ../using_modules
 | 
			
		||||
 | 
			
		||||
require learning.local/using_modules v0.0.0-00010101000000-000000000000
 | 
			
		||||
							
								
								
									
										28
									
								
								hello/hello.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								hello/hello.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,28 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import ( 
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	
 | 
			
		||||
	"learning.local/using_modules"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main () {
 | 
			
		||||
	// Set properties of the predefined Logger, including
 | 
			
		||||
	// the log entry prefix and a flag to disable printing
 | 
			
		||||
	// the time, source fiile, and line number.
 | 
			
		||||
	log.SetPrefix("using_modules: ")
 | 
			
		||||
	log.SetFlags(0)
 | 
			
		||||
	
 | 
			
		||||
	// A slice of names
 | 
			
		||||
	names := []string{"Gladys", "Yoda", "Darth Vader"}
 | 
			
		||||
	
 | 
			
		||||
	// Request greeting messages for the names
 | 
			
		||||
	messages, err := using_modules.Hellos(names)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	// If no error was returned, print the returned map of
 | 
			
		||||
	// messages to the console
 | 
			
		||||
	fmt.Println(messages)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								hello/using_modules.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								hello/using_modules.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,35 @@
 | 
			
		||||
package using_modules
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Hello returns a greeting for the named person
 | 
			
		||||
func Hello(name string) (string, error) {
 | 
			
		||||
	// If no name was given, return an error with a message
 | 
			
		||||
	if name == "" {
 | 
			
		||||
		return "", errors.New("Empty name!")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	message := fmt.Sprintf(randomFormat(), name)
 | 
			
		||||
	return message, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// randomFormat returns one of a set of greeting messages. The returned
 | 
			
		||||
// message is selected at random.
 | 
			
		||||
 | 
			
		||||
func randomFormat() string {
 | 
			
		||||
	// A slice of message formats
 | 
			
		||||
	formats := []string{
 | 
			
		||||
		"Hi, %v. Welcome!",
 | 
			
		||||
		"Great to see you, %v",
 | 
			
		||||
		"Hail, %v! Well met!",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Return a randomly selected message format by specifying
 | 
			
		||||
	// a random index for the slice of formats.
 | 
			
		||||
	return formats[rand.Intn(len(formats))]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								tour_of_go/l1/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								tour_of_go/l1/go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
module learning/tour
 | 
			
		||||
 | 
			
		||||
go 1.24.5
 | 
			
		||||
 | 
			
		||||
require rsc.io/quote v1.5.2
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
 | 
			
		||||
	rsc.io/sampler v1.3.0 // indirect
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										6
									
								
								tour_of_go/l1/go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								tour_of_go/l1/go.sum
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,6 @@
 | 
			
		||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
 | 
			
		||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
			
		||||
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
 | 
			
		||||
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
 | 
			
		||||
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
 | 
			
		||||
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
 | 
			
		||||
							
								
								
									
										10
									
								
								tour_of_go/l1/hello.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								tour_of_go/l1/hello.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
import "rsc.io/quote"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println("Hello Go!")
 | 
			
		||||
	fmt.Println(quote.Go())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										64
									
								
								tour_of_go/l2/functions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								tour_of_go/l2/functions.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,64 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math/cmplx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var c, java, python bool
 | 
			
		||||
var n, m int = 1, 2
 | 
			
		||||
var (
 | 
			
		||||
	ToBe	bool		= false
 | 
			
		||||
	MaxInt	uint64		= 1<<64 - 1
 | 
			
		||||
	z		complex128	= cmplx.Sqrt(-5 + 12i)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func add(x, y int) int {
 | 
			
		||||
	return x + y
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func swap (x, y string) (string, string) {
 | 
			
		||||
	return y, x
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func split(sum int) (x, y int) {
 | 
			
		||||
	x = sum * 4 / 9
 | 
			
		||||
	y = sum - x
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println(add(42,13))
 | 
			
		||||
	x := [3]int{2,1,1}
 | 
			
		||||
	fmt.Println(x)
 | 
			
		||||
 | 
			
		||||
	a, b := swap("hello", "go")
 | 
			
		||||
	fmt.Println(a, b)
 | 
			
		||||
 | 
			
		||||
	fmt.Println(split(17))
 | 
			
		||||
 | 
			
		||||
	var i int
 | 
			
		||||
	fmt.Println(i, c, python, java)
 | 
			
		||||
 | 
			
		||||
	c, java, python := true, true, "no!"
 | 
			
		||||
	k := 3
 | 
			
		||||
	fmt.Println(k, n, m, c, python, java)
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
 | 
			
		||||
	fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
 | 
			
		||||
	fmt.Printf("Type: %T Value: %v\n", z, z)
 | 
			
		||||
 | 
			
		||||
	g := 42
 | 
			
		||||
	f := float64(g)
 | 
			
		||||
	u := uint(f)
 | 
			
		||||
	fmt.Println(g, f, u)
 | 
			
		||||
 | 
			
		||||
	const Pi = 3.14
 | 
			
		||||
	const World = "世界"
 | 
			
		||||
	const Truth = true
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Hello", World)
 | 
			
		||||
	fmt.Println("Happy", Pi, "Day")
 | 
			
		||||
	fmt.Println("Go rules?", Truth)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								tour_of_go/l2/packages.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								tour_of_go/l2/packages.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
	"math"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println("My favorite number is", rand.Intn(10))
 | 
			
		||||
	fmt.Println(math.Pi)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								tour_of_go/l3/\
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								tour_of_go/l3/\
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Sqrt(x float64) float64 {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println(Sqrt(2))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								tour_of_go/l3/conditionals.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								tour_of_go/l3/conditionals.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func pow(x, n, lim float64) float64 {
 | 
			
		||||
	if v := math.Pow(x, n); v < lim {
 | 
			
		||||
		return v
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Printf("%g >= %g\n", v, lim)
 | 
			
		||||
	}
 | 
			
		||||
	return lim
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println(
 | 
			
		||||
		pow(3, 2, 10),
 | 
			
		||||
		pow(3, 3, 20),
 | 
			
		||||
	)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								tour_of_go/l3/ex1.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								tour_of_go/l3/ex1.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Sqrt(x float64) float64 {
 | 
			
		||||
	precision := 0.00001
 | 
			
		||||
	old_z := 0.0
 | 
			
		||||
	z := x
 | 
			
		||||
	iter := 0
 | 
			
		||||
	for (z - old_z) > precision {
 | 
			
		||||
		iter += 1
 | 
			
		||||
		old_z := z
 | 
			
		||||
		z -= (z*z - x) / (2*z)
 | 
			
		||||
		if old_z - z < 0 {
 | 
			
		||||
			if ((old_z - z) * -1) < precision {
 | 
			
		||||
				fmt.Println("Required precision has been reached after", iter, "iterations! Square root of", x , "is", z)
 | 
			
		||||
				return z
 | 
			
		||||
			}
 | 
			
		||||
		} else if (old_z - z) < precision {
 | 
			
		||||
			fmt.Println("Required precision has been reached after", iter, "iterations! Square root of", x , "is", z)
 | 
			
		||||
			return z
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return z
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println(Sqrt(2))
 | 
			
		||||
	fmt.Println(Sqrt(29))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								tour_of_go/l3/loops.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								tour_of_go/l3/loops.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,29 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import ( 
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func sqrt(x float64) string {
 | 
			
		||||
	if x < 0 {
 | 
			
		||||
		return sqrt(-x) + "i"
 | 
			
		||||
	}
 | 
			
		||||
	return fmt.Sprint(math.Sqrt(x))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	sum := 0
 | 
			
		||||
	for i := 0; i < 10; i++ {
 | 
			
		||||
		sum += 1
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(sum)
 | 
			
		||||
 | 
			
		||||
	sum_2 := 1
 | 
			
		||||
	for sum_2 < 1000 {
 | 
			
		||||
		sum_2 += sum_2
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(sum_2)
 | 
			
		||||
 | 
			
		||||
	fmt.Println(sqrt(2), sqrt(-4))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										55
									
								
								tour_of_go/l3/switch.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								tour_of_go/l3/switch.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,55 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"time"
 | 
			
		||||
	"runtime"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func letsDefer(text string) {
 | 
			
		||||
	defer fmt.Println(text)
 | 
			
		||||
	fmt.Print("I have deferred: ")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Print("Go runs on ")
 | 
			
		||||
	switch os := runtime.GOOS; os {
 | 
			
		||||
	case "darwin":
 | 
			
		||||
		fmt.Println("macOS.")
 | 
			
		||||
	case "linux":
 | 
			
		||||
		fmt.Println("Linux.")
 | 
			
		||||
	default:
 | 
			
		||||
		fmt.Printf("%s.\n", os)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("When's Saturday?")
 | 
			
		||||
	today := time.Now().Weekday()
 | 
			
		||||
	switch time.Saturday {
 | 
			
		||||
	case today + 0:
 | 
			
		||||
		fmt.Println("Today.")
 | 
			
		||||
	case today + 1:
 | 
			
		||||
		fmt.Println("Tomorrow.")
 | 
			
		||||
	case today + 2:
 | 
			
		||||
		fmt.Println("In two days.")
 | 
			
		||||
	default:
 | 
			
		||||
		fmt.Println("Too far away.")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	t := time.Now()
 | 
			
		||||
	switch {
 | 
			
		||||
	case t.Hour() < 12:
 | 
			
		||||
		fmt.Println("Good morning!")
 | 
			
		||||
	case t.Hour() < 17:
 | 
			
		||||
		fmt.Println("Good afternoon")
 | 
			
		||||
	default:
 | 
			
		||||
		fmt.Println("Good evening.")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	letsDefer("Some stuff")
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Counting...")
 | 
			
		||||
	for i:=0; i<10; i++ {
 | 
			
		||||
		defer fmt.Println(i)
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println("Done!")
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								tour_of_go/l4/2
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								tour_of_go/l4/2
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var aa [2]string
 | 
			
		||||
 | 
			
		||||
	aa[0] = "Hello"
 | 
			
		||||
	aa[1] = "World"
 | 
			
		||||
	fmt.Println(aa[0], aa[1])
 | 
			
		||||
	fmt.Println(aa)
 | 
			
		||||
 | 
			
		||||
	primes := [6]int{2, 3, 5, 7, 11, 13}
 | 
			
		||||
	fmt.Println(primes)
 | 
			
		||||
 | 
			
		||||
	var s []int = primes[1:4]
 | 
			
		||||
	fmt.Printf("The primes are %d and the slice is %d", primes, s)
 | 
			
		||||
 | 
			
		||||
	// Slices are references to array sections
 | 
			
		||||
	// Changing elements in a slice results in changes to the array
 | 
			
		||||
	// So other slices referencing the same array section will also be changed
 | 
			
		||||
 | 
			
		||||
	names := [4]string{
 | 
			
		||||
		"John",
 | 
			
		||||
		"Paul",
 | 
			
		||||
		"George",
 | 
			
		||||
		"Ringo",
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(names)
 | 
			
		||||
 | 
			
		||||
	a := names[0:2]
 | 
			
		||||
	b := names[1:3]
 | 
			
		||||
	fmt.Println(a, b)
 | 
			
		||||
 | 
			
		||||
	// Now make some changes
 | 
			
		||||
	b[0] = "XXX"
 | 
			
		||||
	fmt.Println(a, b)
 | 
			
		||||
	fmt.Println(names)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								tour_of_go/l4/append.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								tour_of_go/l4/append.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func printSlice(s []int) {
 | 
			
		||||
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
 | 
			
		||||
}
 | 
			
		||||
func main() {
 | 
			
		||||
	var s []int
 | 
			
		||||
	printSlice(s)
 | 
			
		||||
 | 
			
		||||
	s = append(s, 0)
 | 
			
		||||
	printSlice(s)
 | 
			
		||||
 | 
			
		||||
	s = append(s, 1)
 | 
			
		||||
	printSlice(s)
 | 
			
		||||
 | 
			
		||||
	s = append(s, 2, 3, 4)
 | 
			
		||||
	printSlice(s)
 | 
			
		||||
 | 
			
		||||
	s = append(s, 56)
 | 
			
		||||
	printSlice(s)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										72
									
								
								tour_of_go/l4/arrays.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								tour_of_go/l4/arrays.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,72 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var aa [2]string
 | 
			
		||||
 | 
			
		||||
	aa[0] = "Hello"
 | 
			
		||||
	aa[1] = "World"
 | 
			
		||||
	fmt.Println(aa[0], aa[1])
 | 
			
		||||
	fmt.Println(aa)
 | 
			
		||||
 | 
			
		||||
	primes := [6]int{2, 3, 5, 7, 11, 13}
 | 
			
		||||
	fmt.Println(primes)
 | 
			
		||||
 | 
			
		||||
	var s []int = primes[1:4]
 | 
			
		||||
	fmt.Printf("The primes are %d and the slice is %d", primes, s)
 | 
			
		||||
 | 
			
		||||
	// Slices are references to array sections
 | 
			
		||||
	// Changing elements in a slice results in changes to the array
 | 
			
		||||
	// So other slices referencing the same array section will also be changed
 | 
			
		||||
 | 
			
		||||
	names := [4]string{
 | 
			
		||||
		"John",
 | 
			
		||||
		"Paul",
 | 
			
		||||
		"George",
 | 
			
		||||
		"Ringo",
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(names)
 | 
			
		||||
 | 
			
		||||
	a := names[0:2]
 | 
			
		||||
	b := names[1:3]
 | 
			
		||||
	fmt.Println(a, b)
 | 
			
		||||
 | 
			
		||||
	// Now make some changes
 | 
			
		||||
	b[0] = "XXX"
 | 
			
		||||
	fmt.Println(a, b)
 | 
			
		||||
	fmt.Println(names)
 | 
			
		||||
 | 
			
		||||
	// Slice literals - like array literals, but without the length
 | 
			
		||||
	q := []int{2, 3, 5, 7, 11, 13}
 | 
			
		||||
	fmt.Println(q)
 | 
			
		||||
 | 
			
		||||
	r := []bool{true, false, true, true, false, true}
 | 
			
		||||
	fmt.Println(r)
 | 
			
		||||
 | 
			
		||||
	sx := []struct {
 | 
			
		||||
		i int
 | 
			
		||||
		b bool
 | 
			
		||||
	}{
 | 
			
		||||
		{2, true},
 | 
			
		||||
		{3, false},
 | 
			
		||||
		{5, true},
 | 
			
		||||
		{7, true},
 | 
			
		||||
		{11, false},
 | 
			
		||||
		{13, true},
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(sx)
 | 
			
		||||
 | 
			
		||||
	// Slice defaults
 | 
			
		||||
 | 
			
		||||
	ss := []int{2, 3, 5, 7, 11, 13}
 | 
			
		||||
 | 
			
		||||
	ss = ss[1: 4]
 | 
			
		||||
	fmt.Println(ss)
 | 
			
		||||
 | 
			
		||||
	ss = ss[:2]
 | 
			
		||||
	fmt.Println(ss)
 | 
			
		||||
 | 
			
		||||
	ss = ss[1:]
 | 
			
		||||
	fmt.Println(ss)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										41
									
								
								tour_of_go/l4/making-slices.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								tour_of_go/l4/making-slices.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,41 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func printSlice(s []int) {
 | 
			
		||||
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	a := make([]int, 5)
 | 
			
		||||
	printSlice(a)
 | 
			
		||||
 | 
			
		||||
	b := make([]int, 0, 5)
 | 
			
		||||
	printSlice(b)
 | 
			
		||||
 | 
			
		||||
	c := b[:2]
 | 
			
		||||
	printSlice(c)
 | 
			
		||||
 | 
			
		||||
	d := c[2:5]
 | 
			
		||||
	printSlice(d)
 | 
			
		||||
 | 
			
		||||
	// Slices can contain any type including other slices
 | 
			
		||||
	board := [][]string{
 | 
			
		||||
		[]string{"_", "_", "_"},
 | 
			
		||||
		[]string{"_", "_", "_"},
 | 
			
		||||
		[]string{"_", "_", "_"},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	board[0][0] = "X"
 | 
			
		||||
	board[2][2] = "O"
 | 
			
		||||
	board[1][2] = "X"
 | 
			
		||||
	board[1][0] = "O"
 | 
			
		||||
	board[0][2] = "X"
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < len(board); i++ {
 | 
			
		||||
		fmt.Printf("%s\n", strings.Join(board[i], " "))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								tour_of_go/l4/pointers.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								tour_of_go/l4/pointers.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,18 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	i, j := 42, 2701
 | 
			
		||||
 | 
			
		||||
	p := &i
 | 
			
		||||
	fmt.Println(*p)
 | 
			
		||||
	*p = 21
 | 
			
		||||
	fmt.Println(i)
 | 
			
		||||
 | 
			
		||||
	p = &j
 | 
			
		||||
	*p = *p / 37
 | 
			
		||||
	fmt.Println(j)
 | 
			
		||||
 | 
			
		||||
	fmt.Println(i, j)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								tour_of_go/l4/range.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								tour_of_go/l4/range.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	for i, v := range pow {
 | 
			
		||||
		fmt.Printf("2**%d = %d\n", i, v)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// You can skip the index by assigning it to "_"
 | 
			
		||||
	// Or you can skip the value by omitting the 2nd value -> "for i := range XXX"
 | 
			
		||||
	new_pow := make([]int, 10)
 | 
			
		||||
	for i := range new_pow {
 | 
			
		||||
		new_pow[i] = 1 << uint(i)
 | 
			
		||||
	}
 | 
			
		||||
	for _, value := range new_pow {
 | 
			
		||||
		fmt.Printf("%d\n", value)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								tour_of_go/l4/slices-cont.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								tour_of_go/l4/slices-cont.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func printSlice(s []int) {
 | 
			
		||||
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	s := []int{2, 3, 5, 7, 11, 13}
 | 
			
		||||
	printSlice(s)
 | 
			
		||||
 | 
			
		||||
	s = s[:0]
 | 
			
		||||
	printSlice(s)
 | 
			
		||||
 | 
			
		||||
	s = s[:4]
 | 
			
		||||
	printSlice(s)
 | 
			
		||||
 | 
			
		||||
	s = s[2:]
 | 
			
		||||
	printSlice(s)
 | 
			
		||||
 | 
			
		||||
	// Extend beyond capacity
 | 
			
		||||
	// s = s[:10]
 | 
			
		||||
	// printSlice(s)
 | 
			
		||||
 | 
			
		||||
	// Zero value of a slice is nil
 | 
			
		||||
	var ns []int
 | 
			
		||||
	printSlice(ns)
 | 
			
		||||
 | 
			
		||||
	if ns == nil {
 | 
			
		||||
		fmt.Println("nil!!")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								tour_of_go/l4/structs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								tour_of_go/l4/structs.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
type Vertex struct {
 | 
			
		||||
	X int
 | 
			
		||||
	Y int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	v1 = Vertex{1, 2}
 | 
			
		||||
	v2 = Vertex{X: 1}
 | 
			
		||||
	v3 = Vertex{}
 | 
			
		||||
	p = &Vertex{1, 2}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	v := Vertex{1, 2}
 | 
			
		||||
	v.X = 4
 | 
			
		||||
	fmt.Println(v)
 | 
			
		||||
 | 
			
		||||
	o := &v
 | 
			
		||||
	o.X = 12
 | 
			
		||||
	fmt.Printf("New vertex value is %d\n", v)
 | 
			
		||||
 | 
			
		||||
	fmt.Println(v1, p, v2, v3)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										3
									
								
								using_modules/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								using_modules/go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
module learning.local/using_modules
 | 
			
		||||
 | 
			
		||||
go 1.24.5
 | 
			
		||||
							
								
								
									
										54
									
								
								using_modules/using_modules.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								using_modules/using_modules.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,54 @@
 | 
			
		||||
package using_modules
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Hello returns a greeting for the named person
 | 
			
		||||
func Hello(name string) (string, error) {
 | 
			
		||||
	// If no name was given, return an error with a message
 | 
			
		||||
	if name == "" {
 | 
			
		||||
		return "", errors.New("empty name")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	message := fmt.Sprintf(randomFormat(), name)
 | 
			
		||||
	return message, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Hellos retruns a map that associates each of the named people
 | 
			
		||||
// with a greeting message.
 | 
			
		||||
func Hellos(names []string) (map[string]string, error) {
 | 
			
		||||
	// A map to associate names with messages
 | 
			
		||||
	messages := make(map[string]string)
 | 
			
		||||
	// Loop through the received slice of names, calling
 | 
			
		||||
	// the Hello function to get a message for each name.
 | 
			
		||||
	for _, name := range names {
 | 
			
		||||
		message, err := Hello(name)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		// In the map, associate the terieved message with
 | 
			
		||||
		// the name.
 | 
			
		||||
		messages[name] = message
 | 
			
		||||
	}
 | 
			
		||||
	return messages, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// randomFormat returns one of a set of greeting messages. The returned
 | 
			
		||||
// message is selected at random.
 | 
			
		||||
 | 
			
		||||
func randomFormat() string {
 | 
			
		||||
	// A slice of message formats
 | 
			
		||||
	formats := []string{
 | 
			
		||||
		"Hi, %v. Welcome!",
 | 
			
		||||
		"Great to see you, %v!",
 | 
			
		||||
		"Hail, %v! Well met!",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Return a randomly selected message format by specifying
 | 
			
		||||
	// a random index for the slice of formats.
 | 
			
		||||
	return formats[rand.Intn(len(formats))]
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								using_modules/using_modules_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								using_modules/using_modules_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
package using_modules
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
	"regexp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TestHelloName calls greetings.Hello with a name, checking
 | 
			
		||||
// for a valid return value.
 | 
			
		||||
func TestHelloName(t *testing.T) {
 | 
			
		||||
	name := "Gladys"
 | 
			
		||||
	want := regexp.MustCompile(`\b`+name+`\b`)
 | 
			
		||||
	msg, err := Hello("Gladys")
 | 
			
		||||
	if !want.MatchString(msg) || err != nil {
 | 
			
		||||
		t.Errorf(`Hello("Gladys") = %q, %v, want match for %#q, nill`, msg, err, want)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TestHelloEmpty calls greeting.Hello with an empty string,
 | 
			
		||||
// checking for an error
 | 
			
		||||
func TestHelloEmpty(t *testing.T) {
 | 
			
		||||
	msg, err := Hello("")
 | 
			
		||||
	if msg != "" || err == nil {
 | 
			
		||||
		t.Errorf(`Hello("") = %q, %v, want "", error`, msg, err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user