This document provides an outline and summary of topics covered in Part 2 of Rob Pike's "The Go Programming Language" presentation, including exercises, composite types (arrays, slices, maps), methods, and interfaces. The outline includes exercises on Fibonacci numbers, an explanation of arrays and how they differ from C arrays, how slices reference arrays and can be resized, how maps are another reference type indexed by keys, and an overview of structs as a way to define the layout of memory with composite types. Anonymous fields are also discussed as a way to embed one struct in another.
I am Christopher Hemmingway. I am a Computer Science Assignment Expert at programminghomeworkhelp.com. I hold a Master's in Computer Science, Princeton University, Princeton. I have been helping students with their homework for the past 10 years. I solve assignments related to Computer Science.
Visit programminghomeworkhelp.com or email support@programminghomeworkhelp.com.You can also call on +1 678 648 4277 for any assistance with Computer Science assignments.
I am Christopher Hemmingway. I am a Computer Science Assignment Expert at programminghomeworkhelp.com. I hold a Master's in Computer Science, Princeton University, Princeton. I have been helping students with their homework for the past 10 years. I solve assignments related to Computer Science.
Visit programminghomeworkhelp.com or email support@programminghomeworkhelp.com.You can also call on +1 678 648 4277 for any assistance with Computer Science assignments.
The objective of the Level 5 Diploma in Information Technology is to provide learners with an excellent foundation for a career in a range of organisations. It designed to ensure that each learner is ‘business ready’: a confident, independent thinker with a detailed knowledge of Information Technology, and equipped with the skills to adapt rapidly to change.
A basic introduction to the R programming language. No prior programming knowledge needed. Download to access embedded files. Slide content includes:
Part zero: Getting started (Interacting with R)
Part one: Objects (Vectors, Matrices, Character arrays)
Part two: Data manipulation (Analysing data, T-test)
Part three: External data (Reading data into R, ANOVA )
Part four: Packages and libraries (Installing new packages into R)
Part five: Scripts (Using pre-written code)
Part six: Logic (programming, other functions in R)
Please use the comments to list any issues with the slides or suggest improvements
An invited talk by Xavier Leroy explaining the current state of OCaml at the OCaml Users and Developers Workshop 2014. http://ocaml.org/meetings/ocaml/2014/
learn how to program with python
from scratch to be an expert
use the link in the first slide to get the full course
here is the link if you want : https://oke.io/JdxdUl
the link of the documentation : https://oke.io/J6DeY3
link to download python : https://oke.io/BENgRLR2
Python is an interpreted, high-level, and general-purpose programming language.
It is an easy to learn general-purpose programing language.
It is a platform-independent programing language, which means it can be used on any machine and in any operating system.
It has a simple syntax
Python is a case sensitive language.
It is an interrupted language.
It is free to use and even free for commercial products.
This session for beginners introduces tf.data APIs for creating data pipelines by combining various "lazy operators" in tf.data, such as filter(), map(), batch(), zip(), flatmap(), take(), and so forth.
Familiarity with method chaining and TF2 is helpful (but not required). If you are comfortable with FRP, the code samples in this session will be very familiar to you.
The objective of the Level 5 Diploma in Information Technology is to provide learners with an excellent foundation for a career in a range of organisations. It designed to ensure that each learner is ‘business ready’: a confident, independent thinker with a detailed knowledge of Information Technology, and equipped with the skills to adapt rapidly to change.
A basic introduction to the R programming language. No prior programming knowledge needed. Download to access embedded files. Slide content includes:
Part zero: Getting started (Interacting with R)
Part one: Objects (Vectors, Matrices, Character arrays)
Part two: Data manipulation (Analysing data, T-test)
Part three: External data (Reading data into R, ANOVA )
Part four: Packages and libraries (Installing new packages into R)
Part five: Scripts (Using pre-written code)
Part six: Logic (programming, other functions in R)
Please use the comments to list any issues with the slides or suggest improvements
An invited talk by Xavier Leroy explaining the current state of OCaml at the OCaml Users and Developers Workshop 2014. http://ocaml.org/meetings/ocaml/2014/
learn how to program with python
from scratch to be an expert
use the link in the first slide to get the full course
here is the link if you want : https://oke.io/JdxdUl
the link of the documentation : https://oke.io/J6DeY3
link to download python : https://oke.io/BENgRLR2
Python is an interpreted, high-level, and general-purpose programming language.
It is an easy to learn general-purpose programing language.
It is a platform-independent programing language, which means it can be used on any machine and in any operating system.
It has a simple syntax
Python is a case sensitive language.
It is an interrupted language.
It is free to use and even free for commercial products.
This session for beginners introduces tf.data APIs for creating data pipelines by combining various "lazy operators" in tf.data, such as filter(), map(), batch(), zip(), flatmap(), take(), and so forth.
Familiarity with method chaining and TF2 is helpful (but not required). If you are comfortable with FRP, the code samples in this session will be very familiar to you.
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxfaithxdunce63732
CS 360 LAB 3: STRINGS, FUNCTIONS, AND METHODS
Objective: The purpose of this lab is to get you comfortable with using the Python functions and modules.
Note: Python has a lot of modules that come with the language, which collectively are called the Python Standard Library. The Python documentation contains the specifications for all the functions in the library. You can get to it from the link on the course website, or by going here:
http://docs.python.org/library/
1) Strings
Strings have many handy methods, whose specifications can be found at the following URL:
http://docs.python.org/2/library/stdtypes.html#string-methods
Strings are a built-in type, so although there is a module named string, you do not need it for basic string operations.
To reference substrings (or even individual characters) we can use the bracket notation shown in class. Remember Python counts characters starting from zero|for example, s[1] is the second character in s.
One of the things to remember about strings is that they are immutable. Methods or sequence operations return new strings; they never modify the original string object.
Evaluating string Expressions. Before starting with the table below, enter the following assignment statement into the Python shell:
s = "Hello World!"
Once you have done that, you will use the string stored in s to _ll out the table below.
Expression Expected
Expected Value
Calculated value
Reason for calculated value
s[2]
s[15]
s[1:5]
s[:5]
s[5:]
"e" in s
"x" in s
s.index("e")
s.index("x")
s.index("l", 5)
where to start looking)
s.find("e")
s.find("x")
s.islower()
s[1:5].islower()
Prepared By: Kajal Nusratullah Page 1
Programming Languages CS 360
Lab Tutorial # 1 Week no 1 and 2
Visit the following link and Install Python 3.
www.python.org
Beginning with python
Type the following text to the right of the Python prompt and press the Enter key:
>>> print ("Hello, Python!")
Assigning Values to Variables:
Python variables do not have to be explicitly declared to reserve memory space. The declaration happens
automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is
the value stored in the variable. For example:
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print counter
print miles
print name
Here, 100, 1000.0 and "John" are the values assigned to counter, miles and name variables, respectively. While
running this program, this will produce the following result:
100
1000.0
John
Write down a program in C and Java that will
declare the same variables, assign the same
values and discuss th.
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
Homework Assignment – Array Technical Document
Write a technical document that describes the structure and use of arrays. The document should
be 3 to 5 pages and include an Introduction section, giving a brief synopsis of the document and
arrays, a Body section, describing arrays and giving an annotated example of their use as a
programming construct, and a conclusion to revisit important information about arrays described
in the Body of the document. Some suggested material to include:
Declaring arrays of various types
Array pointers
Printing and processing arrays
Sorting and searching arrays
Multidimensional arrays
Indexing arrays of various dimension
Array representation in memory by data type
Passing arrays as arguments
If you find any useful images on the Internet, you can use them as long as you cite the source in
end notes.
Solution
Array is a collection of variables of the same type that are referenced by a common name.
Specific elements or variables in the array are accessed by means of index into the array.
If taking about C, In C all arrays consist of contiguous memory locations. The lowest address
corresponds to the first element in the array while the largest address corresponds to the last
element in the array.
C supports both single and multi-dimensional arrays.
1) Single Dimension Arrays:-
Syntax:- type var_name[size];
where type is the type of each element in the array, var_name is any valid identifier, and size is
the number of elements in the array which has to be a constant value.
*Array always use zero as index to first element.
The valid indices for array above are 0 .. 4, i.e. 0 .. number of elements - 1
For Example :- To load an array with values 0 .. 99
int x[100] ;
int i ;
for ( i = 0; i < 100; i++ )
x[i] = i ;
To determine to size of an array at run time the sizeof operator is used. This returns the size in
bytes of its argument. The name of the array is given as the operand
size_of_array = sizeof ( array_name ) ;
2) Initialisg array:-
Arrays can be initialised at time of declaration in the following manner.
type array[ size ] = { value list };
For Example :-
int i[5] = {1, 2, 3, 4, 5 } ;
i[0] = 1, i[1] = 2, etc.
The size specification in the declaration may be omitted which causes the compiler to count the
number of elements in the value list and allocate appropriate storage.
For Example :- int i[ ] = { 1, 2, 3, 4, 5 } ;
3) Multidimensional array:-
Multidimensional arrays of any dimension are possible in C but in practice only two or three
dimensional arrays are workable. The most common multidimensional array is a two
dimensional array for example the computer display, board games, a mathematical matrix etc.
Syntax :type name [ rows ] [ columns ] ;
For Example :- 2D array of dimension 2 X 3.
int d[ 2 ] [ 3 ] ;
A two dimensional array is actually an array of arrays, in the above case an array of two integer
arrays (the rows) each with three elements, and is stored row-wise in memory.
For Example :- Program to fill .
The Array is the most commonly used Data Structure.
An array is a collection of data elements that are of the same type (e.g., a collection of integers, collection of characters, collection of doubles).
OR
Array is a data structure that represents a collection of the same types of data.
The values held in an array are called array elements
An array stores multiple values of the same type – the element type
The element type can be a primitive type or an object reference
Therefore, we can create an array of integers, an array of characters, an array of String objects, an array of Coin objects, etc.
COMPLEX AND USER DEFINED TYPESPlease note that the material on t.docxdonnajames55
COMPLEX AND USER DEFINED TYPES
Please note that the material on this website is not intended to be exhaustive.
This is intended as a summary and supplementary material to required textbook.
INTRODUCTION
Previously we have been dealing with C++ built-in types, that is: types that the language already supports. In addition, all of the types we have been using so far (with two exceptions: string, and the reference type: &) have been simple types. In this module we will begin to look at complex types, sometimes called structured types. Complex types allow the programmer to define their own types, which we will call user defined types.
C++ Built-In Types
Classification
Name
Examples
Can be unsigned?
Simple
Integral
char
yes
"
"
short
yes
"
"
int
yes
"
"
long
yes
"
"
bool
no
"
Enumerated
enum
"
"
Decimal
float
"
"
"
double
"
"
"
long double
"
Address
Pointer
* (asterisk)
"
"
Reference
& (ampersand)
"
Complex
Array
[ ] (brackets)
"
"
Structured
struct
"
"
Union
union
"
"
Abstract
class
"
In this module we will explain enumerations, structures, and unions. In future modules we will cover arrays and classes.
Some classifications above may come as a surprise. How can char be an integral type (and even unsigned), and why is bool considered an integral type?
Characters in C++ are considered 8-bit bytes, and hence they are represented internally as integers in base 2: just 0s and 1s. If you want an integer that only ranges between 0 and 255 (unsigned), or between -128 and 127 (signed), you can use a char. All of the arithmetic operations can be used on char.
A character (char) was originally taken to be one of the symbols on your keyboard, and to represent those only need 7 bits are needed. Extended character sets must use 2 or more bytes to represent characters not on your keyboard (such a representation scheme is known as Unicode), for instance, Cyrillic characters or some mathematic symbols.
We have already seen that the Boolean true and false are really internally represented as 1 and 0, respectively. However, arithmetic operations will not work on bool.
You will also notice that a type we have used repeatedly (string) is not listed above. The strings we have been using are really of type class, which will be introduced another module.
ENUMERATIONS
Enumerated types are user-defined; that is, you get to choose what you want to enumerate. Here are some simple examples:
enum PrimaryColors {RED, YELLOW, BLUE}; enum Days {SUN, MON, YUE, WED, THU, FRI, SAT};
C++ will internally represent these types using 0, 1, 2, 3, .... Notice that the standard convention is to use upper-case alphas in the enumeration, as they are really very similar to constants. You can override the internal defaults with something like the following:
enum PrimaryColors {RED = 5, YELLOW = 10, BLUE = 20};
The default internal representation is often very convenient, as the enumeration symbol strings can then be used as indices of arrays, a future topic.
STRUCT.
Visual programming (VP) refers to a method of programming that uses a graphical interface to create and modify code, rather than typing out code manually. Unlike traditional text-based programming, visual programming uses visual elements to represent code. Some graphical elements you can use when creating programs with VP include:
Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL.
ImpoImport this, that, and the other thing: custom importersrt not for_yZoom Quiet
Mr. Brett Cannon
in PyCon2010-USA-Atlanta
45min ◊◊◊ Advanced
categories: core
Since Python 2.3, the ability has existed to customize the import process so that one can support code stored in alternative formats, e.g. zipimport and its support of importing code from a zip file. The aim of this talk is to make sure you understand how the custom import mechanism works and how to write your own custom importers using importlib from Python 3.1.
Import this, that, and the other thing: custom importersZoom Quiet
Mr. Brett Cannon
in PyCon2010-USA-Atlanta
45min ◊◊◊ Advanced
Friday 11:45am, Centennial I
categories: core
Since Python 2.3, the ability has existed to customize the import process so that one can support code stored in alternative formats, e.g. zipimport and its support of importing code from a zip file. The aim of this talk is to make sure you understand how the custom import mechanism works and how to write your own custom importers using importlib from Python 3.1.
GraphRAG is All You need? LLM & Knowledge GraphGuy Korland
Guy Korland, CEO and Co-founder of FalkorDB, will review two articles on the integration of language models with knowledge graphs.
1. Unifying Large Language Models and Knowledge Graphs: A Roadmap.
https://arxiv.org/abs/2306.08302
2. Microsoft Research's GraphRAG paper and a review paper on various uses of knowledge graphs:
https://www.microsoft.com/en-us/research/blog/graphrag-unlocking-llm-discovery-on-narrative-private-data/
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Albert Hoitingh
In this session I delve into the encryption technology used in Microsoft 365 and Microsoft Purview. Including the concepts of Customer Key and Double Key Encryption.
UiPath Test Automation using UiPath Test Suite series, part 4DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 4. In this session, we will cover Test Manager overview along with SAP heatmap.
The UiPath Test Manager overview with SAP heatmap webinar offers a concise yet comprehensive exploration of the role of a Test Manager within SAP environments, coupled with the utilization of heatmaps for effective testing strategies.
Participants will gain insights into the responsibilities, challenges, and best practices associated with test management in SAP projects. Additionally, the webinar delves into the significance of heatmaps as a visual aid for identifying testing priorities, areas of risk, and resource allocation within SAP landscapes. Through this session, attendees can expect to enhance their understanding of test management principles while learning practical approaches to optimize testing processes in SAP environments using heatmap visualization techniques
What will you get from this session?
1. Insights into SAP testing best practices
2. Heatmap utilization for testing
3. Optimization of testing processes
4. Demo
Topics covered:
Execution from the test manager
Orchestrator execution result
Defect reporting
SAP heatmap example with demo
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Accelerate your Kubernetes clusters with Varnish CachingThijs Feryn
A presentation about the usage and availability of Varnish on Kubernetes. This talk explores the capabilities of Varnish caching and shows how to use the Varnish Helm chart to deploy it to Kubernetes.
This presentation was delivered at K8SUG Singapore. See https://feryn.eu/presentations/accelerate-your-kubernetes-clusters-with-varnish-caching-k8sug-singapore-28-2024 for more details.
Epistemic Interaction - tuning interfaces to provide information for AI supportAlan Dix
Paper presented at SYNERGY workshop at AVI 2024, Genoa, Italy. 3rd June 2024
https://alandix.com/academic/papers/synergy2024-epistemic/
As machine learning integrates deeper into human-computer interactions, the concept of epistemic interaction emerges, aiming to refine these interactions to enhance system adaptability. This approach encourages minor, intentional adjustments in user behaviour to enrich the data available for system learning. This paper introduces epistemic interaction within the context of human-system communication, illustrating how deliberate interaction design can improve system understanding and adaptation. Through concrete examples, we demonstrate the potential of epistemic interaction to significantly advance human-computer interaction by leveraging intuitive human communication strategies to inform system design and functionality, offering a novel pathway for enriching user-system engagements.
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
Here is something new! In our next Connector Corner webinar, we will demonstrate how you can use a single workflow to:
Create a campaign using Mailchimp with merge tags/fields
Send an interactive Slack channel message (using buttons)
Have the message received by managers and peers along with a test email for review
But there’s more:
In a second workflow supporting the same use case, you’ll see:
Your campaign sent to target colleagues for approval
If the “Approve” button is clicked, a Jira/Zendesk ticket is created for the marketing design team
But—if the “Reject” button is pushed, colleagues will be alerted via Slack message
Join us to learn more about this new, human-in-the-loop capability, brought to you by Integration Service connectors.
And...
Speakers:
Akshay Agnihotri, Product Manager
Charlie Greenberg, Host
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
As AI technology is pushing into IT I was wondering myself, as an “infrastructure container kubernetes guy”, how get this fancy AI technology get managed from an infrastructure operational view? Is it possible to apply our lovely cloud native principals as well? What benefit’s both technologies could bring to each other?
Let me take this questions and provide you a short journey through existing deployment models and use cases for AI software. On practical examples, we discuss what cloud/on-premise strategy we may need for applying it to our own infrastructure to get it to work from an enterprise perspective. I want to give an overview about infrastructure requirements and technologies, what could be beneficial or limiting your AI use cases in an enterprise environment. An interactive Demo will give you some insides, what approaches I got already working for real.
JMeter webinar - integration with InfluxDB and GrafanaRTTS
Watch this recorded webinar about real-time monitoring of application performance. See how to integrate Apache JMeter, the open-source leader in performance testing, with InfluxDB, the open-source time-series database, and Grafana, the open-source analytics and visualization application.
In this webinar, we will review the benefits of leveraging InfluxDB and Grafana when executing load tests and demonstrate how these tools are used to visualize performance metrics.
Length: 30 minutes
Session Overview
-------------------------------------------
During this webinar, we will cover the following topics while demonstrating the integrations of JMeter, InfluxDB and Grafana:
- What out-of-the-box solutions are available for real-time monitoring JMeter tests?
- What are the benefits of integrating InfluxDB and Grafana into the load testing stack?
- Which features are provided by Grafana?
- Demonstration of InfluxDB and Grafana using a practice web application
To view the webinar recording, go to:
https://www.rttsweb.com/jmeter-integration-webinar
State of ICS and IoT Cyber Threat Landscape Report 2024 previewPrayukth K V
The IoT and OT threat landscape report has been prepared by the Threat Research Team at Sectrio using data from Sectrio, cyber threat intelligence farming facilities spread across over 85 cities around the world. In addition, Sectrio also runs AI-based advanced threat and payload engagement facilities that serve as sinks to attract and engage sophisticated threat actors, and newer malware including new variants and latent threats that are at an earlier stage of development.
The latest edition of the OT/ICS and IoT security Threat Landscape Report 2024 also covers:
State of global ICS asset and network exposure
Sectoral targets and attacks as well as the cost of ransom
Global APT activity, AI usage, actor and tactic profiles, and implications
Rise in volumes of AI-powered cyberattacks
Major cyber events in 2024
Malware and malicious payload trends
Cyberattack types and targets
Vulnerability exploit attempts on CVEs
Attacks on counties – USA
Expansion of bot farms – how, where, and why
In-depth analysis of the cyber threat landscape across North America, South America, Europe, APAC, and the Middle East
Why are attacks on smart factories rising?
Cyber risk predictions
Axis of attacks – Europe
Systemic attacks in the Middle East
Download the full report from here:
https://sectrio.com/resources/ot-threat-landscape-reports/sectrio-releases-ot-ics-and-iot-security-threat-landscape-report-2024/
Elevating Tactical DDD Patterns Through Object CalisthenicsDorra BARTAGUIZ
After immersing yourself in the blue book and its red counterpart, attending DDD-focused conferences, and applying tactical patterns, you're left with a crucial question: How do I ensure my design is effective? Tactical patterns within Domain-Driven Design (DDD) serve as guiding principles for creating clear and manageable domain models. However, achieving success with these patterns requires additional guidance. Interestingly, we've observed that a set of constraints initially designed for training purposes remarkably aligns with effective pattern implementation, offering a more ‘mechanical’ approach. Let's explore together how Object Calisthenics can elevate the design of your tactical DDD patterns, offering concrete help for those venturing into DDD for the first time!
Monitoring Java Application Security with JDK Tools and JFR Events
Go courseday2
1. The
Go
Programming Language
Part 2
Rob Pike
r@google.com
(updated June, 2011)
Friday, June 10, 2011 1
2. Today’s Outline
Exercise
any questions?
Composite types
structures, arrays, slices, maps
Methods
they're not just for structs any more
Interfaces
Friday, June 10, 2011 2
3. Exercise
Any questions?
One nugget:
n0, n1 = n0+n1, n0
or for a general binary operator
n0, n1 = op(n0, n1), n0
Friday, June 10, 2011 3
5. Arrays
Arrays are quite different from C arrays; more like
Pascal arrays. (Slices, the next topic, act a little
more like C arrays.)
var ar [3]int
declares ar to be an array of 3 integers, initially all
set to zero.
Size is part of the type.
Built-in function len reports size:
len(ar) == 3
Friday, June 10, 2011 5
6. Arrays are values
Arrays are values, not implicit pointers as in C. You
can take an array's address, yielding a pointer to
the array (for instance, to pass it efficiently to a
function):
func f(a [3]int) { fmt.Println(a) }
func fp(a *[3]int) { fmt.Println(a) }
func main() {
var ar [3] int
f(ar) // passes a copy of ar
fp(&ar) // passes a pointer to ar
}
Output (Print and friends know about arrays):
[0 0 0]
&[0 0 0]
Friday, June 10, 2011 6
7. Array literals
All the composite types have similar syntax for
creating values. For arrays it look like this:
Array of 3 integers:
[3]int{1, 2, 3}
Array of 10 integers, first three not zero:
[10]int{ 1, 2, 3}
Don't want to count? Use ... for the length:
[...]int{1, 2, 3}
Don't want to initialize them all? Use key:value pairs:
[10]int{2:1, 3:1, 5:1, 7:1}
Friday, June 10, 2011 7
8. Pointers to array literals
You can take the address of an array literal to get a
pointer to a newly created instance:
func fp(a *[3]int) { fmt.Println(a) }
func main() {
for i := 0; i < 3; i++ {
fp(&[3]int{i, i*i, i*i*i})
}
}
Output:
&[0 0 0]
&[1 1 1]
&[2 4 8]
Friday, June 10, 2011 8
10. Slices
A slice is a reference to a section of an array.
Slices are used much more often than plain arrays.
A slice is very cheap. (More about this soon.)
A slice type looks like an array type without a size:
var a []int
Built-in len(a) returns the number of elements.
Create a slice by "slicing" an array or slice:
a = ar[7:9]
Valid indexes of a will then be 0 and 1; len(a)==2.
Friday, June 10, 2011 10
11. Slice shorthands
When slicing, first index defaults to 0:
ar[:n] means the same as ar[0:n].
Second index defaults to len(array/slice):
ar[n:] means the same as ar[n:len(ar)].
Thus to create a slice from an array:
ar[:] means the same as ar[0:len(ar)].
Friday, June 10, 2011 11
12. A slice references an array
Conceptually:
type Slice struct {
base *elemType // pointer to 0th element
len int // num. of elems in slice
cap int // num. of elems available
}
Array:
ar: 7 1 5 4 3 8 7 2 11 5 3
Slice:
a=ar[7:9]: base=&ar[7] len=2 cap=4
Friday, June 10, 2011 12
13. Making slices
Slice literals look like array literals without a size:
var slice = []int{1,2,3,4,5}
What this does is create an array of length 5 and
then create a slice to refer to it.
We can also allocate a slice (and underlying array)
with the built-in function make:
var s100 = make([]int, 100) // slice: 100 ints
Why make not new? Because we need to make a slice,
not just allocate the memory. Note make([]int, 10)
returns []int while new([]int) returns *[]int.
Use make to create slices, maps, and channels.
Friday, June 10, 2011 13
14. Slice capacity
A slice refers to an underlying array, so there may be
elements off the end of the slice that are present in the
array.
The built-in function cap (capacity) reports how long
the slice could possibly grow. After
var ar = [10]int{0,1,2,3,4,5,6,7,8,9}
var a = ar[5:7] // reference to subarray {5,6}
len(a) is 2 and cap(a) is 5. We can "reslice":
a = a[0:4] // reference to subarray {5,6,7,8}
len(a) is now 4 but cap(a) is still 5.
Friday, June 10, 2011 14
15. Resizing a slice
Slices can be used like growable arrays. Allocate one
using make with two numbers - length and capacity -
and reslice as it grows:
var sl = make([]int, 0, 100) // len 0, cap 100
func appendToSlice(i int, sl []int) []int {
if len(sl) == cap(sl) { error(...) }
n := len(sl)
sl = sl[0:n+1] // extend length by 1
sl[n] = i
return sl
}
Thus sl's length is always the number of elements, but
it grows as needed.
This style is cheap and idiomatic in Go.
Friday, June 10, 2011 15
16. Slices are cheap
Feel free to allocate and resize slices as required. They
are cheap to pass around; no need to allocate.
Remember they are references, so underlying storage
can be modified.
For instance, I/O uses slices, not counts:
func Read(fd int, b []byte) int
var buffer [100]byte
for i := 0; i < 100; i++ {
// Fill buffer one byte at a time.
Read(fd, buffer[i:i+1]) // no allocation here
}
Split a buffer:
header, data := buf[:n], buf[n:]
Strings can be sliced too, with similar efficiency.
Friday, June 10, 2011 16
18. Maps
Maps are another reference type. They are declared
like this:
var m map[string]float64
This declares a map indexed with key type string
and value type float64. It is analogous to the C++
type *map<string,float64> (note the *).
Given a map m, len(m) returns the number of keys.
Friday, June 10, 2011 18
19. Map creation
As with a slice, a map variable refers to nothing; you
must put something in it before it can be used.
Three ways:
1) Literal: list of colon-separated key:value pairs
m = map[string]float64{"1":1, "pi":3.1415}
2) Creation
m = make(map[string]float64) // make not new
3) Assignment
var m1 map[string]float64
m1 = m // m1 and m now refer to same map
Friday, June 10, 2011 19
20. Indexing a map
(Next few examples all use
m = map[string]float64{"1":1, "pi":3.1415}
)
Access an element as a value; if not present, get
zero value for the map's value type:
one := m["1"]
zero := m["not present"] // Sets zero to 0.0.
Set an element (setting twice updates value for key)
m["2"] = 2
m["2"] = 3 // mess with their heads
Friday, June 10, 2011 20
21. Testing existence
To test if a key is present in the map, we can use a
multi-value assignment, the "comma ok" form:
m = map[string]float64{"1":1, "pi":3.1415}
var value float64
var present bool
value, present = m[x]
or idiomatically
value, ok := m[x] // hence, the "comma ok" form
If x is present in the map, sets the boolean to true
and the value to the entry for the key. If not, sets
the boolean to false and the value to the zero for
its type.
Friday, June 10, 2011 21
22. Deleting
Deleting an entry in the map is a multi-variate
assignment to the map entry:
m = map[string]float64{"1":1.0, "pi":3.1415}
var keep bool
var value float64
var x string = f()
m[x] = v, keep
If keep is true, assigns v to the map; if keep is false,
deletes the entry for key x. So to delete an entry,
m[x] = 0, false // deletes entry for x
Friday, June 10, 2011 22
23. For and range
The for loop has a special syntax for iterating over
arrays, slices, maps (and more, as we'll see tomorrow).
m := map[string]float64{"1":1.0, "pi":3.1415}
for key, value := range m {
fmt.Printf("key %s, value %gn", key, value)
}
With only one variable in the range, get the key:
for key = range m {
fmt.Printf("key %sn", key)
}
Variables can be assigned or declared using := .
For arrays and slices, get index and value.
Friday, June 10, 2011 23
24. Range over string
A for using range on a string loops over Unicode code
points, not bytes. (Use []byte for bytes, or use a
standard for). The string is assumed to contain UTF-8.
The loop
s := "[u00ffu754c]"
for i, c := range s {
fmt.Printf("%d:%q ", i, c) // %q for 'quoted'
}
Prints 0:'[' 1:'ÿ' 3:'界' 6:']'
If erroneous UTF-8 is encountered, the character is set
to U+FFFD and the index advances by one byte.
Friday, June 10, 2011 24
26. Structs
Structs should feel very familiar: simple
declarations of data fields.
var p struct {
x, y float64
}
More usual:
type Point struct {
x, y float64
}
var p Point
Structs allow the programmer to define the layout
of memory.
Friday, June 10, 2011 26
27. Structs are values
Structs are values and new(StructType) returns a
pointer to a zero value (memory all zeros).
type Point struct {
x, y float64
}
var p Point
p.x = 7
p.y = 23.4
var pp *Point = new(Point)
*pp = p
pp.x = Pi // sugar for (*pp).x
There is no -> notation for structure pointers. Go
provides the indirection for you.
Friday, June 10, 2011 27
28. Making structs
Structs are values so you can make a zeroed one
just by declaring it.
You can also allocate one with new.
var p Point // zeroed value
pp := new(Point) // idiomatic allocation
Struct literals have the expected syntax.
p = Point{7.2, 8.4}
p = Point{y:8.4, x:7.2}
pp = &Point{7.2, 8.4} // idiomatic
pp = &Point{} // also idiomatic; == new(Point)
As with arrays, taking the address of a struct literal
gives the address of a newly created value.
These examples are constructors.
Friday, June 10, 2011 28
29. Exporting types and fields
The fields (and methods, coming up soon) of a
struct must start with an Uppercase letter to be
visible outside the package.
Private type and fields:
type point struct { x, y float64 }
Exported type and fields:
type Point struct { X, Y float64 }
Exported type with mix of fields:
type Point struct {
X, Y float64 // exported
name string // not exported
}
You may even have a private type with exported
fields. Exercise: when is that useful?
Friday, June 10, 2011 29
30. Anonymous fields
Inside a struct, you can declare fields, such as
another struct, without giving a name for the field.
These are called anonymous fields and they act as if
the inner struct is simply inserted or "embedded"
into the outer.
This simple mechanism provides a way to derive
some or all of your implementation from another
type or types.
An example follows.
Friday, June 10, 2011 30
31. An anonymous struct field
type A struct {
ax, ay int
}
type B struct {
A
bx, by float64
}
B acts as if it has four fields, ax, ay, bx, and by. It's
almost as if B is {ax, ay int; bx, by float64}.
However, literals for B must be filled out in detail:
b := B{A{1, 2}, 3.0, 4.0}
fmt.Println(b.ax, b.ay, b.bx, b.by)
Prints 1 2 3 4
Friday, June 10, 2011 31
32. Anonymous fields have type as name
But it's richer than simple interpolation of the fields:
B also has a field A. The anonymous field looks like
a field whose name is its type.
b := B{A{ 1, 2}, 3.0, 4.0}
fmt.Println(b.A)
Prints {1 2}. If A came from another package, the
field would still be called A:
import "pkg"
type C struct { pkg.A }
...
c := C {pkg.A{1, 2}}
fmt.Println(c.A) // not c.pkg.A
Friday, June 10, 2011 32
33. Anonymous fields of any type
Any named type, or pointer to one, may be
used in an anonymous field and it may
appear at any location in the struct.
type C struct {
x float64
int
string
}
c := C{3.5, 7, "hello"}
fmt.Println(c.x, c.int, c.string)
Prints 3.5 7 hello
Friday, June 10, 2011 33
34. Conflicts and hiding
If there are two fields with the same name (possibly
a type-derived name), these rules apply:
1) An outer name hides an inner name.
This provides a way to override a field/method.
2) If the same name appears twice at the same level,
it is an error if the name is used by the program.
(If it's not used, it doesn't matter.)
No rules to resolve the ambiguity; it must be fixed.
Friday, June 10, 2011 34
35. Conflict examples
type A struct { a int }
type B struct { a, b int }
type C struct { A; B }
var c C
Using c.a is an error: is it c.A.a or c.B.a?
type D struct { B; b float64 }
var d D
Using d.b is OK: it's the float64, not d.B.b
Can get at the inner b by D.B.b.
Friday, June 10, 2011 35
37. Methods on structs
Go has no classes, but you can attach methods to
any type. Yes, (almost) any type. The methods are
declared, separate from the type declaration, as
functions with an explicit receiver. The obvious
struct case:
type Point struct { x, y float64 }
// A method on *Point
func (p *Point) Abs() float64 {
return math.Sqrt(p.x*p.x + p.y*p.y)
}
Note: explicit receiver (no automatic this), in this
case of type *Point, used within the method.
Friday, June 10, 2011 37
38. Methods on struct values
A method does not require a pointer as a receiver.
type Point3 struct { x, y, z float64 }
// A method on Point3
func (p Point3) Abs() float64 {
return math.Sqrt(p.x*p.x + p.y*p.y + p.z*p.z)
}
This is a bit expensive, because the Point3 will always
be passed to the method by value, but it is valid Go.
Friday, June 10, 2011 38
39. Invoking a method
Just as you expect.
p := &Point{ 3, 4 }
fmt.Print(p.Abs()) // will print 5
A non-struct example:
type IntVector []int
func (v IntVector) Sum() (s int) {
for _, x := range v { // blank identifier!
s += x
}
return
}
fmt.Println(IntVector{1, 2, 3}.Sum())
Friday, June 10, 2011 39
40. Ground rules for methods
Methods are attached to a named type, say Foo, and
are statically bound.
The type of a receiver in a method can be either *Foo
or Foo. You can have some Foo methods and some
*Foo methods.
Foo itself cannot be a pointer type, although the
methods can have receiver type *Foo.
The type Foo must be defined in the same package as
all its methods.
Friday, June 10, 2011 40
41. Pointers and values
Go automatically indirects/dereferences values for
you when invoking methods.
For instance, even though a method has receiver type
*Point you can invoke it on an addressable value of
type Point.
p1 := Point{ 3, 4 }
fmt.Print(p1.Abs()) // sugar for (&p1).Abs()
Similarly, if methods are on Point3 you can use a
value of type *Point3:
p3 := &Point3{ 3, 4, 5 }
fmt.Print(p3.Abs()) // sugar for (*p3).Abs()
Friday, June 10, 2011 41
42. Methods on anonymous fields
Naturally, when an anonymous field is embedded in
a struct, the methods of that type are embedded as
well - in effect, it inherits the methods.
This mechanism offers a simple way to emulate
some of the effects of subclassing and inheritance.
Friday, June 10, 2011 42
43. Anonymous field example
type Point struct { x, y float64 }
func (p *Point) Abs() float64 { ... }
type NamedPoint struct {
Point
name string
}
n := &NamedPoint{Point{3, 4}, "Pythagoras"}
fmt.Println(n.Abs()) // prints 5
Friday, June 10, 2011 43
44. Overriding a method
Overriding works just as with fields.
type NamedPoint struct {
Point
name string
}
func (n *NamedPoint) Abs() float64 {
return n.Point.Abs() * 100.
}
n := &NamedPoint{Point{3, 4}, "Pythagoras"}
fmt.Println(n.Abs()) // prints 500
Of course you can have multiple anonymous fields
with various types - a simple version of multiple
inheritance. The conflict resolution rules keep
things simple, though.
Friday, June 10, 2011 44
45. Another example
A more compelling use of an anonymous field.
type Mutex struct { ... }
func (m *Mutex) Lock() { ... }
type Buffer struct {
data [100]byte
Mutex // need not be first in Buffer
}
var buf = new(Buffer)
buf.Lock() // == buf.Mutex.Lock()
Note that Lock's receiver is (the address of) the
Mutex field, not the surrounding structure.
(Contrast to subclassing or Lisp mix-ins.)
Friday, June 10, 2011 45
46. Other types
Methods are not just for structs. They can be
defined for any (non-pointer) type.
The type must be defined in your package though.
You can't write a method for int but you can
declare a new int type and give it methods.
type Day int
var dayName = []string {
"Monday", "Tuesday", "Wednesday", ...
}
func (day Day) String() string {
return dayName[day]
}
Friday, June 10, 2011 46
47. Other types
Now we have an enumeration-like type that knows
how to print itself.
const (
Monday Day = iota
Tuesday
Wednesday
// ...
)
var day = Tuesday
fmt.Printf("%q", day.String()) // prints "Tuesday"
Friday, June 10, 2011 47
48. Print understands String methods
By techniques to be divulged soon, fmt.Print and
friends can identify values that implement the method
String as we defined for type Day. Such values are
automatically formatted by invoking the method.
Thus:
fmt.Println(0, Monday, 1, Tuesday)
prints 0 Monday 1 Tuesday.
Println can tell a plain 0 from a 0 of type Day.
So define a String method for your types and they will
print nicely with no more work.
Friday, June 10, 2011 48
49. Visibility of fields and methods
Review:
Go is very different from C++ in the area of visibility.
Go's rules:
1) Go has package scope (C++ has file scope).
2)Spelling determines exported/local (pub/priv).
3)Structs in the same package have full access to one
another's fields and methods.
4) Local type can export its fields and methods.
5) No true subclassing, so no notion of protected.
These simple rules seem to work well in practice.
Friday, June 10, 2011 49
51. Watch closely
We are about to look at Go's most unusual aspect:
the interface.
Please leave your preconceptions at the door.
Friday, June 10, 2011 51
52. Introduction
So far, all the types we have examined have been
concrete: they implement something.
There is one more type to consider: the interface
type. It is completely abstract; it implements
nothing. Instead, it specifies a set of properties an
implementation must provide.
Interface as a concept is very close to that of Java,
and Java has an interface type, but the interface
value concept of Go is novel.
Friday, June 10, 2011 52
53. Definition of an interface
The word interface is a bit overloaded in Go:
there is the concept of an interface, and there is
an interface type, and then there are values of that
type. First, the concept.
Definition:
An interface is a set of methods.
To turn it around, the methods implemented by a
concrete type such as a struct form the interface
of that type.
Friday, June 10, 2011 53
54. Example
We saw this simple type before:
type Point struct { x, y float64 }
func (p *Point) Abs() float64 { ... }
The interface of type Point has the method:
Abs() float64
It's not
func (p *Point) Abs() float64
because the interface abstracts away the receiver.
We embedded Point in a new type, NamedPoint;
NamedPoint has the same interface.
Friday, June 10, 2011 54
55. The interface type
An interface type is a specification of an interface, a
set of methods implemented by some other types.
Here's a simple one, with only one method:
type AbsInterface interface {
Abs() float64 // receiver is implied
}
This is a definition of the interface implemented by
Point, or in our terminology,
Point implements AbsInterface
Also,
NamedPoint and Point3 implement AbsInterface
Methods are written inside the interface declaration.
Friday, June 10, 2011 55
56. An example
type MyFloat float64
func (f MyFloat) Abs() float64 {
if f 0 { return float64(-f) }
return f
}
MyFloat implements AbsInterface even though
float64 does not.
(Aside: MyFloat is not a boxing of float64; its
representation is identical to float64.)
Friday, June 10, 2011 56
57. Many to many
An interface may be implemented by an arbitrary
number of types. AbsInterface is implemented by
any type that has a method with signature
Abs() float64, regardless of what other methods
that type may have.
A type may implement an arbitrary number of
interfaces. Point implements at least these two:
type AbsInterface interface { Abs() float64 }
type EmptyInterface interface { }
And perhaps more, depending on its methods.
Every type implements EmptyInterface. That will
come in handy.
Friday, June 10, 2011 57
58. Interface value
Once a variable is declared with interface type, it may
store any value that implements that interface.
var ai AbsInterface
pp := new(Point)
ai = pp // OK: *Point has Abs()
ai = 7. // compile-time err:
// float64 has no Abs()
ai = MyFloat(-7.) // OK: MyFloat has Abs()
ai = Point{ 3, 4 }
fmt.Printf(ai.Abs()) // method call
Prints 5.
Note: ai is not a pointer! It is an interface value.
Friday, June 10, 2011 58
59. In memory
ai is not a pointer! It's a multiword data structure.
receiver method
ai:
value table ptr
At different times it has different value and type:
ai = Point{3,4} (a *Point at address 0xff1234):
(*Point) Abs() float64
0xff1234
...
ai = MyFloat(-7.):
(MyFloat) Abs() float64
-7.
...
Friday, June 10, 2011 59
60. Three important facts
1) Interfaces define sets of methods. They are pure
and abstract: no implementation, no data fields. Go
has a clear separation between interface and
implementation.
2) Interface values are just that: values. They
contain any concrete value that implements all the
methods defined in the interface. That concrete
value may or may not be a pointer.
3) Types implement interfaces just by having
methods. They do not have to declare that they do
so. For instance, every type implements the empty
interface, interface{}.
Friday, June 10, 2011 60
61. Example: io.Writer
Here is the actual signature of fmt.Fprintf:
func Fprintf(w io.Writer, f string, a ... interface{})
(n int, error os.Error)
It doesn't write to a file, it writes to something of type
io.Writer, that is, Writer defined in the io package:
type Writer interface {
Write(p []byte) (n int, err os.Error)
}
Fprintf can therefore be used to write to any type
that has a canonical Write method, including files,
pipes, network connections, and...
Friday, June 10, 2011 61
62. Buffered I/O
... a write buffer. This is from the bufio package:
type Writer struct { ... }
bufio.Writer implements the canonical Write method.
func (b *Writer) Write(p []byte) (n int, err os.Error)
It also has a factory: give it an io.Writer, it will return
a buffered io.Writer in the form of a bufio.Writer:
func NewWriter(wr io.Writer) (b *Writer, err os.Error)
And of course, os.File implements Writer too.
Friday, June 10, 2011 62
63. Putting it together
import (
bufio; fmt; os
)
func main() {
// unbuffered
fmt.Fprintf(os.Stdout, %s, , hello)
// buffered: os.Stdout implements io.Writer
buf := bufio.NewWriter(os.Stdout)
// and now so does buf.
fmt.Fprintf(buf, %sn, world!)
buf.Flush()
}
Buffering works for anything that Writes.
Feels almost like Unix pipes, doesn't it? The
composability is powerful; see crypto packages.
Friday, June 10, 2011 63
64. Other public interfaces in io
The io package has:
Reader
Writer
ReadWriter
ReadWriteCloser
These are stylized interfaces but obvious: they capture
the functionality of anything implementing the
functions listed in their names.
This is why we can have a buffered I/O package with
an implementation separate from the I/O itself: it both
accepts and provides interface values.
Friday, June 10, 2011 64
65. Comparison
In C++ terms, an interface type is like a pure abstract
class, specifying the methods but implementing none
of them.
In Java terms, an interface type is much like a Java
interface.
However, in Go there is a major difference:
A type does not need to declare the interfaces it
implements, nor does it need to inherit from an
interface type. If it has the methods, it implements
the interface.
Some other differences will become apparent.
Friday, June 10, 2011 65
66. Anonymous fields work too
type LockedBufferedWriter struct {
Mutex // has Lock and Unlock methods
bufio.Writer // has Write method
}
func (l *LockedBufferedWriter) Write(p []byte)
(nn int, err os.Error) {
l.Lock()
defer l.Unlock()
return l.Writer.Write(p) // inner Write()
}
LockedBufferedWriter implements io.Writer, but also,
through the anonymous Mutex,
type Locker interface { Lock(); Unlock() }
Friday, June 10, 2011 66
67. Example: HTTP service
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
This is the interface defined by the HTTP server
package. To serve HTTP, define a type that
implements this interface and connect it to the server
(details omitted).
type Counter struct {
n int // or could just say type Counter int
}
func (ctr *Counter) ServeHTTP(w http.ResponseWriter,
req *http.Request) {
fmt.Fprintf(w, counter = %dn, ctr.n)
ctr.n++
}
Friday, June 10, 2011 67
68. A function (type) that serves HTTP
func notFound(w http.ResponseWriter, req *http.Request){
w.SetHeader(Content-Type, text/plain; +
charset=utf-8)
w.WriteHeader(StatusNotFound)
w.WriteString(404 page not foundn)
}
Now we define a type to implement ServeHTTP:
type HandlerFunc func(http.ResponseWriter, *http.Request)
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter,
req *http.Request) {
f(w, req) // the receiver's a func; call it
}
Convert function to attach method, implement the
interface:
var Handle404 = HandlerFunc(notFound)
Friday, June 10, 2011 68
69. Containers the empty interface
Sketch of the implementation of vectors. (In practice,
tend to use raw slices instead, but this is informative):
type Element interface {}
// Vector is the container itself.
type Vector []Element
// At() returns the i'th element.
func (p *Vector) At(i int) Element {
return p[i]
}
Vectors can contain anything because any type
implements the empty interface. (In fact every element
could be of different type.)
Friday, June 10, 2011 69
70. Type assertions
Once you put something into a Vector, it's stored as
an interface value. Need to unbox it to get the
original back: use a type assertion. Syntax:
interfaceValue.(typeToExtract)
Will fail if type is wrong - but see next slide.
var v vector.Vector
v.Set(0, 1234.) // stored as interface val
i := v.At(0) // retrieved as interface{}
if i != 1234. {} // compile-time err
if i.(float64) != 1234. {} // OK
if i.(int) != 1234 {} // run-time err
if i.(MyFloat) != 1234. {} // err: not MyFloat
Type assertions always execute at run time. Compiler
rejects assertions guaranteed to fail.
Friday, June 10, 2011 70
71. Interface to interface conversion
So far we've only moved regular values into and out of
interface values, but interface values that contain the
appropriate methods can also be converted.
In effect, it's the same as unboxing the interface value
to extract the underlying concrete value, then boxing
it again for the new interface type.
The conversion's success depends on the underlying
value, not the original interface type.
Friday, June 10, 2011 71
72. Interface to interface example
Given:
var ai AbsInterface
type SqrInterface interface { Sqr() float64 }
var si SqrInterface
pp := new(Point) // say *Point has Abs, Sqr
var empty interface{}
These are all OK:
empty = pp // everything satisfies empty
ai = empty.(AbsInterface) // underlying value
// implements Abs()
// (runtime failure otherwise)
si = ai.(SqrInterface) // *Point has Sqr()
// even though AbsInterface doesn't
empty = si // *Point implements empty set
// Note: statically checkable
// so type assertion not necessary.
Friday, June 10, 2011 72
73. Testing with type assertions
Can use comma ok type assertions to test a value for
type.
elem := vector.At(0)
if i, ok := elem.(int); ok {
fmt.Printf(int: %dn, i)
} else if f, ok := elem.(float64); ok {
fmt.Printf(float64: %gn, f)
} else {
fmt.Print(unknown typen)
}
Friday, June 10, 2011 73
74. Testing with a type switch
Special syntax:
switch v := elem.(type) { // literal keyword type
case int:
fmt.Printf(is int: %dn, v)
case float64:
fmt.Printf(is float64: %gn, v)
default:
fmt.Print(unknown typen)
}
Friday, June 10, 2011 74
75. Does v implement m()?
Going one step further, can test whether a value
implements a method.
type Stringer interface { String() string }
if sv, ok := v.(Stringer); ok {
fmt.Printf(implements String(): %sn,
sv.String()) // note: sv not v
}
This is how Print etc. check if type can print itself.
Friday, June 10, 2011 75
76. Reflection and . . .
There is a reflection package (reflect) that builds on
these ideas to let you examine values to discover their
type. Too intricate to describe here but Printf etc.
use it to analyze the its arguments.
func Printf(format string, args ...interface{})
(n int, err os.Error)
Inside Printf, the args variable becomes a slice of the
specified type, i.e. []interface{}, and Printf uses the
reflection package to unpack each element to analyze
its type.
More about variadic functions in the next section.
Friday, June 10, 2011 76
77. Reflection and Print
As a result, Printf and its friends know the actual
types of their arguments. Because they know if the
argument is unsigned or long, there is no %u or %ld,
only %d.
This is also how Print and Println can print the
arguments nicely without a format string.
There is also a %v (value) format that gives default
nice output from Printf for values of any type.
fmt.Printf(%v %v %v %v, -1, hello,
[]int{1,2,3}, uint64(456))
Prints -1 hello [1 2 3] 456.
In fact, %v is identical to the formatting done by Print
and Println.
Friday, June 10, 2011 77
79. Variadic functions: . . .
Variable-length parameter lists are declared with the
syntax ...T, where T is the type of the individual
arguments. Such arguments must be the last in the
argument list. Within the function the variadic
argument implicitly has type []T.
func Min(args ...int) int {
min := int(^uint(0)1) // largest possible int
for _, x := range args { // args has type []int
if min x { min = x }
}
return min
}
fmt.Println(Min(1,2,3), Min(-27), Min(), Min(7,8,2))
Prints 1 -27 2147483647 2
Friday, June 10, 2011 79
80. Slices into variadics
The argument becomes a slice. What if you want to
pass the slice as arguments directly? Use ... at the
call site. (Only works for variadics.)
Recall: func Min(args ...int) int
Both these invocations return -2:
Min(1, -2, 3)
slice := []int{1, -2, 3}
Min(slice...) // ... turns slice into arguments
This, however, is a type error:
Min(slice)
because slice is of type []int while Min's arguments
must be individually int. The ... is mandatory.
Friday, June 10, 2011 80
81. Printf into Error
We can use the ... trick to wrap Printf or one of its
variants to create our own custom error handler.
func Errorf(fmt string, args ...interface{}) {
fmt.Fprintf(os.Stderr, MyPkg: +fmt+n, args...)
os.Exit(1)
}
We can use it like this:
if err := os.Chmod(file, 0644); err != nil {
Errorf(couldn't chmod %q: %s, file, err)
}
Output (which includes newline):
MyPkg: couldn't chmod foo.bar: permission denied
Friday, June 10, 2011 81
82. Append
The built-in function append, which is used to grow
slices, is variadic. It has (in effect) this signature:
append(s []T, x ...T) []T
where s is a slice and T is its element type. It returns a
slice with the elements x appended to s.
slice := []int{1, 2, 3}
slice = append(slice, 4, 5, 6)
fmt.Println(slice)
prints [1 2 3 4 5 6]
When possible, append will grow the slice in place.
Friday, June 10, 2011 82
83. Appending a slice
If we want to append a whole slice, rather than
individual elements, we again use ... at the call site.
slice := []int{1, 2, 3}
slice2 := []int{4, 5, 6}
slice = append(slice, slice2...) // ... is necessary
fmt.Println(slice)
This example also prints [1 2 3 4 5 6]
Friday, June 10, 2011 83
85. Exercise: Day 2
Look at the http package.
Write an HTTP server to present pages in the
file system, but transformed somehow,
perhaps rot13, perhaps something more
imaginative. Can you make the
transformation substitutable? Can you work
in your Fibonacci program somehow?
Friday, June 10, 2011 85
86. Next lesson
Concurrency and communication
Friday, June 10, 2011 86
87. The
Go
Programming Language
Part 2
Rob Pike
r@google.com
(updated June 2011)
Friday, June 10, 2011 87