What is the purpose of the capitals in https://play.golang.org/p/vQs1J-MnZFE


(Cherolyn Lexvold) #1

https://play.golang.org/p/vQs1J-MnZFE

Specifically,
“First”
“Last”
“Age”


(Yamil Bracho) #2

In Go, capital items indicate they can be exported. That means it can be accesed from another package


(Jay Ts) #3

Here’s a bit more in the language specification:
https://golang.org/ref/spec#Exported_identifiers


(Cherolyn Lexvold) #4

What’s the difference between import and export. I’m pretty sure that import means that I use some prewritten package in my program. So does export mean that I am sending part of my code somewhere?


(Yamil Bracho) #5

No, it is related if items in the package can be accesed from another.
Import specify which packages you will use. export means if you can acess or not to an item (in another programming languages it is called private and public access)


(Jay Ts) #6

Take a look at this: https://play.golang.org/p/bOficacMcY9

It uses a new feature of the Go Playground. Now you can include more than one file of source code. Where you see

-- go.mod --

The part after that would normally be in a file called go.mod, until you see

-- say/say.go --

and the part after that would be in a file called say.go inside a directory (folder) called say. So there are three files represented in this Go Playground example.

In say.go, another package called say is defined. In that, there are two functions, Hello() and Name(). To use them from package main, you need to capitalize them, as shown.

In package main, you need an import statement to gain access to package say, and then functions in that package can be called as say.Hello() and say.Name().

Try changing Hello() to hello() in either or both of package main and package say and see what happens.

In package main, package say is imported by using the import statement.

In package say, the functions are exported by capitalizing their names. Other things besides functions may also be exported from packages.


(Cherolyn Lexvold) #7

Studying this…


(Cherolyn Lexvold) #8

The files being go.mod, say, and say.go?

What’s module playground?

import "playground/say" 

This is interesting. playground is a package? say is a specified part of that package? Oh. You say “say” is a package.

%s is new to me

%s the uninterpreted bytes of the string or slice (Go doc)
What does uninterpreted mean here?

fmt.Printf("Hello, ") Here, why did you use Printf rather than Pritnln?

How can I find the package say on golang.org?

Maybe I need a good definition of a package. I did a little searching with google and I think I’m getting it.

"The part after that "… do you mean module playground by this?

Try changing Hello() to hello() in either or both of package main and package say and see what happens…did it. Fun! and interesting.

I think I’m done for now.

A little more… can you give me definitions for import and export?


(Jay Ts) #9

Don’t worry about the module stuff and filenames in my example. I used the Go Playground because I needed a way to answer your question about imports and exports.

This reminds me to nag you again about learning to use Terminal (actually, the Bash shell) so you can start using Go for real. The Playground is just a sandbox to play around in, and works differently than an actual programming environment.

By learning Go without first knowing things that support it, it is like building the second story of a house first.


(Cherolyn Lexvold) #10

I’ve forgotten what to do with the terminal