CS代写 • Course overview

• Course overview
• Introduction to programming
• Review: The object-oriented paradigm in Java
• Imperative and concurrent programming paradigm: Go.

Copyright By PowCoder代写 加微信 powcoder

• Logic paradigm: Prolog.
• Functional paradigm: Scheme.

System Programming: Go
Design of the Language Keywords and Types Variables and Functions Structured types

Introduction to Go
• Started as a part-time project of three programming and OS veterans at Google
– (Java HotSpot Virtual Machine)
– (Part of Unix team at Bell Labs)
– (Part of Unix team at Bell Labs, One of the Inventors of Unix, C and Plan 9)
• Timeline
– November 10, 2009 officially announced for Linux and Mac with a BSD license
– Windows port announced by November 22
– First public release on January 8, 2010
• Premier source of information is https://golang.org/

Go Programming Language
• Go is designed to be the “C of the 21st century” • Go unifies conflicting language features
– Efficiency, speed and safety of strongly typed languages (like C/C++ )
– Fast compilation and building (like Java/C#)
– Ease of programming of a dynamically typed language (as Python)
• Goal is to “make programming fun again” – Type-safe
– Memory-safe
• Pointers but no pointer arithmetic

Paradigms in Go
• Imperative Language
• Not quite object-oriented
– No classes and inheritance
– But with interfaces and forms of polymorphism • Fundamental aspect of a functional programming • Support for parallel and concurrent programming
– Systems programming
– High-performance computing

Missing Concepts
• No function or operator overloading – simpler design
• No implicit type conversions – avoid bugs and confusion
• No dynamic code loading or dynamic libraries
– avoid complex dependency on installed environment
• No generics or templates • No exceptions
– but alternative recover after panic • No classes and type inheritance
• No assertions
• Have a Garbage collection

Go Execution
• About 20% slower than C
• 2x faster than Java and 70% less
memory-consumption

Elements of Go
• Code is structured in packages
– The package main is the default package
• all go code belongs to a package (similar to C++ namespaces)
– Go uses Unicode (UTF-8) everywhere
– Identifiers are case sensitive, begin with a letter or _ followed by 0 or more letters or Unicode digits
• 25 keywords (that’s all!) • 36 predefined identifiers

IDEs for Go
– Open source cross platform IDE
– configurable builds (projects), debugging
– available for windows, linux, MacOS and OpenBSD. – https://sourceforge.net/projects/liteide/
• Atom package
– https://atom.io/packages/go-plus
• GoLand – IntelliJ IDEA Plugin
– https://www.jetbrains.com/go/
– https://plugins.jetbrains.com/plugin/5047-go-
language- golang-org-support-plugin
• Eclipse with goclipse plugin – seems somewhat stale – http://goclipse.github.io/
• There is always vim or emacs

Go Programming Environment
• Go expects your code to adhere to a particular directory structure
– Wokspace will have three directories
– Executables go in ($MY_WORKSPACE)/bin/
– Packages (libraries) go in ($MY_WORKSPACE)/pkg/
– Source code goes at some level under ($MY_WORKSPACE)/src/

Hello World in Go
package main // default package
import “fmt” // import of a package
func main() {
fmt.Println(“Hello World”)

Hello World in Go
package main // default package import “fmt” // import of a package
func main() {
fmt.Println(“Hello World”)
– Comments use // for one-line comments
– Section can be commented out with /* */ –Nospecialcharactertoendaline(semicolonisrequired toput
several statements in one line)
– Exported functions from a package are always
capitalized
– Entry point main has no arguments and no return

Keywords and Predefined Identifiers
• Keywords
– break default func interface select case defer go map struct chan else goto package switch const fallthrough
if range type continue for return var
• Predefined identifiers (functions and types) – Constants
•false, true, iota
– Functions
•append cap close complex copy delete imag len make new panic print println real recover

• int, int8, int16, int32
• byte, uint, uint16, uint32, uint64
• float32, float64
• complex, complex64, complex128
• and pointers…

• Asequenceofarbitrarybytes(Runes/ UTF-8 Unicode characters)

Defining and Using Variables in Go
package main import “fmt”
func main() {
var a float64 = 8.8
fmt.Printf(“Result: %f”, a) }

• Variables can be defined in two different ways – defined and default initialized
•var x int makes x a default (0) initialized integer – initializing definition
•x := 1 makes x an integer and initializes it from the assignment
– sometimes we want to combine it
•var x int16 = 5 makes x an initialized int16
• Can factor (group) variable definitions – most often used with globals
var a( float64 = 8.8

Defining and Using Variables/constants in Go
package main import “fmt”
const pi= 3.1416
var x int = 5 // global variable
func main() {
var a float64 = 8.8
fmt.Printf(“Result: %f”, a) }

Defining and Using Variables and function in Go
package main import “fmt”
const pi= 3.1416
var x int = 5 // global variable
func main() {
var ( // grouping or “factoring the keyword”
a float64 = 8.8 ) b float64
fmt.Printf(“Result: %f”, b) }
func foo(z float64) float64 {
u := 3.3 // intializing declaration
return u*z }

Types of arguments and returns are at the end Multiple arguments and multiple returns General syntax
funcfunctionName(parameter_list) (return_value_list){ function-body
– parameter_list is of the form (param1 type1, param2 type2, …)
– return_value_list is of the form (ret1 type1, ret2 type2, …) or is unnamed (type1, type2, …)
– [return_values] either have to be specified if unnamed or can be associated by name
return [return_values]

Example: Function with Multiple Returns
func main() { var (s int
s, d = plusminus(7,9) fmt.Printf(“Result= %d and %d”, s , d)
func plusminus(a int, b int) (sum int, difference int) { sum = a+b
difference = a-b return

Functions with an Error Code
• Errors are signalled with error codes
func bmi(height float64, weight float64) (float64, bool) {
if height > 0.0 {
return weight / (height*height), true
return 0.0, false }
• Test the returned error code
// if initialisation; condition
if value, ok := bmi(1.50, 55); ok {
fmt.Printf(“BMI: %f\n”, value) }

Functions with Parameters that are Functions
• Functions can be passed like variables – Example: Callback
type Point struct { x float64
y float64 }
func Distance(p1 Point, p2 Point) (distance float64){ distance = math.Sqrt(math.Pow(p1.x – p2.x, 2.0) +
math.Pow(p1.y – p2.y, 2.0)) return
func calc(p1 Point, p2 Point,
d func(Point, Point)(float64))(float64) { return d(p1,p2)

Passing a Function
• Functions can be passed like variables
– Example: Callback with a Function and a Lambda (anonymous function)
func main() {
a := Point{2.,4.} b := Point{5.,9.}
dist := calc(a,b,Distance)
fmt.Printf(“result= %f\n”, dist)
dist = calc(a,b,
func(p Point, q Point)float64{
return math.Abs(p.x-q.x)+math.Abs(p.y-q.y) })
fmt.Printf(“result= %f\n”, dist)

Lambda Functions – Closure
• Lambdafunctionbecalleddirectlyorassignedtoa variable, e.g., direct call
func main() {
x, y := 32, 64
func(a int) {
fmt.Println(“a = “, a) fmt.Println(“x = “, x) fmt.Println(“y = “, y)
fmt.Println(“x = “, x)
fmt.Println(“y = “, y) }

Variables and Type Categories
• Threecategoriesofdatatypes
– elementary or primitive •int, float, bool, string
structured or composite
• struct, array, slice, map, channel
– interfaces
• only describe the behavior of a type
– Variables are initialized to their zero type by default – Structures have a default value of nil

A pointer is the address of that in memory.
& operator returns the address of the variable or the function
* operator returns the data at the address.

func main() {
var ptr *int // pointer var i int
*ptr= i+5 // dereference fmt.Printf(“result= %d\n”, i)

Pointers and Structures
type Point struct { x int
p1= Point{1,2}
p2= Point{2,7}
pp= &Point{3,4} // pointer to Point
func main() {
// dynamic allocation
ptr1:= new(Point) ptr2:= &Point{9,8}

Pointers and Structures
type Point struct {
x int y int
func complement(p *Point) {
p.x, p.y = -p.y, -p.x
func main() {
pt := Point{8, 1} complement(&pt)
} fmt.Printf(“Result= %d and %d\n”, pt.x , pt.y)
– A dereference operator is here not required
• no pointer arithmetic

Arrays in Go
package main import “fmt”
func mean(tab [5]int) (meanVal float64) {
// for index, value := range collection for _, value := range tab {
} meanVal+= (float64)(value)
meanVal /= (float64)(len(tab))
func main() {
var table = [5]int{3, 4, 8, 9, 2} m := mean(table) // pass by value fmt.Printf(“result= %f\n”, m)

Arrays in Go
package main import “fmt”
func mean(tab [5]int) (meanVal float64) {
// for index, value := range collection for _, value := range tab {
} meanVal+= (float64)(value)
meanVal /= (float64)(len(tab))
func main() {
table := […]int{3, 4, 8, 9, 2} m := mean(table) // pass by value fmt.Printf(“result= %f\n”, m)

Slices in Go
• Asliceisareferencetoacontiguousregioninmemory – it refers to contiguous elements in an array
• as such it “shares” the elements
• SlicesarecommonlyusedinGoinsteadofcopying arrays
• Aslicehasagivendimensionandcapacity
– the capacity is determined by the underlying array
• a slice can not be bigger than the underlying array
• Creatingaslice
– var slice []int = array[start:end]
– slice := make([]int, 10, 100) // size and capicity
• Familiarfrompython

Example: Slices
func mean(tab []int) (meanVal float64){
// for index, value := range collection for _, value := range tab {
} meanVal+= (float64)(value)
meanVal /= (float64)(len(tab))
func main() {
var table = [5]int{3, 4, 8, 9, 2}
m := mean(table[:]) // all elements
fmt.Printf(“result= %f\n”, m)
m = mean(table[2:]) // elements 2 to the end
fmt.Printf(“result= %f\n”, m)
m = mean(table[1:3]) // 2 elements from 1 up to 3
fmt.Printf(“result= %f\n”, m)

• DesignoftheLanguage
– Multi-paradigm
– Targeted at system programming
– Efficient to code but also efficient to run
• KeywordsandTypes
• VariablesandFunctions
– main, multiple returns, error codes • Structuredtypes
– structures, arrays, slices

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com