Subroutines, parm passing


(Charles N. Cordaro) #1

Hello

How do you code subroutines/functions with parameter passing like in other languages like Python ?

Also how do you write an array to file ? Does the array have to be converted to bytes by a Go library function ?


(Jay Ts) #2

Hi Charles,

Here are three free online books to help you get started:

https://gobyexample.com/
https://www.miek.nl/go/
https://www.golang-book.com/books/intro

The author of the third book also has a rewrite that is published by O’Reilly Media. There are many other non-free books as well. I suggest you take a look at Go in Action, by William Kennedy (publisher: Manning). I’ve been watching his video course on advanced-level Go programming, Ultimate Go Programming (2nd edition), and I’m very impressed with his knowledge of programming in general, and Go in particular.

Also, you can take the Go Tour if you haven’t already:

https://tour.golang.org/welcome/1

And if you have access to it, I wrote an introductory article on Go that was published this month in Linux Journal.

The way functions are defined in Go is like this:

func name(parameters) returned result(s) {
function body
}

There are many ways to write the contents of an array to a file. It depends on what you are doing with the data.


(Charles N. Cordaro) #3

Hi

Thanks for links and book suggestions. I looked up functions and parm passing in the gobyexample web site. Answered my question.

What is best way to write an integer array to a text file where the array elements are all a byte length ?


(Jay Ts) #4

Hi Charles,

First, in Go we generally do things with slices rather than arrays. As you learn a little Go, you will understand why.

I generally avoid “what is the best way to …?” questions because the best way depends on so many things. But a very simple way to write a slice of bytes to a file is using ioutil.WriteFile()

https://golang.org/pkg/io/ioutil/#WriteFile

package main

import "io/ioutil"

func main() {
        const filename = "test.out"
        var int_array []byte = []byte{ 0x23, 0x45, 0x67 }

        ioutil.WriteFile(filename,int_array,0644)
}

The best way for your specific need is probably something different!


(Charles N. Cordaro) #5

I am working with an array that has 786,000 elements. and is defined as integer. ( var xyz [] int ) Your example defines the int_array as []byte.
My program has math operations for the xyz array. xyz[ j ] = a+b. where a and b are integers. Could I use int_array[ j ] = a+b with the same result ? .Do I have to use a for loop int_array[ j ] = xyz[ j ] ?

Does 0x23, 0x45, ox67 mean each array element ? I have hundreds of thousands.

What does the 0644 in the write statement do ?


(Jay Ts) #6

0644 is simply octal permissions flags, as used on Unix/Linux/POSIX systems.

http://permissions-calculator.org/decode/0644/

I used hex numbers (0x23 and others) so you can examine the output file with a hexdump program to see that it was written correctly.

I used byte (equivalent to uint8) type because you said you had integers that fit into bytes. I assumed all of your integers are small enough. If you need to write integers that require more than 8 bits (such as int16 or larger), then you will need to use a different method. For example, take a look at binary.Write():

https://golang.org/pkg/encoding/binary/#Write


(Charles N. Cordaro) #7

My integer array has standard Ascii code numbers ( 0 to 127 ). I want to write the array to a text file to use in another program. The array is defined as xyz [] int It is loaded by a for loop with xyz[ j ] = (a + B) mod 127. Could I code ioutil.WriteFileFile(xyz) or does the xyz array have to be converted to a byte array ? If so how can I do this ?
I did find the statement di := []byte(“Hello World”) in the web site GOExamples. I, however, am not working with a string.


(Jay Ts) #8

Hi Charles,

If you are using the array only for integers less than 128, then it would be simpler if you use a slice of byte-sized integers instead of an array of ints. You can use the byte type (which is an alias for uint8):

package main

import "io/ioutil"

var xyz []byte

const filename = "ascii.out"

func main() {
        var j byte

        for j = 0; j < 128; j++ {
                xyz = append(xyz,j)
        }
        ioutil.WriteFile(filename,xyz,0644)
}

$ go run ascii.go
$ ls -l ascii.out

-rw-r--r-- 1 jay jay 128 Oct 11 11:26 ascii.out

$ hd ascii.out

00000000  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|
00000010  10 11 12 13 14 15 16 17  18 19 1a 1b 1c 1d 1e 1f  |................|
00000020  20 21 22 23 24 25 26 27  28 29 2a 2b 2c 2d 2e 2f  | !"#$%&'()*+,-./|
00000030  30 31 32 33 34 35 36 37  38 39 3a 3b 3c 3d 3e 3f  |0123456789:;<=>?|
00000040  40 41 42 43 44 45 46 47  48 49 4a 4b 4c 4d 4e 4f  |@ABCDEFGHIJKLMNO|
00000050  50 51 52 53 54 55 56 57  58 59 5a 5b 5c 5d 5e 5f  |PQRSTUVWXYZ[\]^_|
00000060  60 61 62 63 64 65 66 67  68 69 6a 6b 6c 6d 6e 6f  |`abcdefghijklmno|
00000070  70 71 72 73 74 75 76 77  78 79 7a 7b 7c 7d 7e 7f  |pqrstuvwxyz{|}~.|
00000080

(Charles N. Cordaro) #9

Good morning

Could I append a calculated number such as R = ( A + B ) mod 10 where A,B and R are integers and replace xyz = append(( xyz, j ) with append(xyz, R) ?   Is it possible to define x as a byte var and do math such as A + B ?   If I define A and B as []byte, can I load them with numbers that evolve math equations ?

(Jay Ts) #10

Charles,

Yes to the first two, and I don’t understand the third question.

The byte type is an alias for uint8, which is an unsigned 8-bit integer.

I suggest you find a good introductory book or video class on Go and study the language.

Some suggestions:

Introducing Go (O’Reilly) - introductory book only, but easy to read

Go in Action (Manning) - a more detailed book

Video Courses - Introduction to Go Programming and Intermediate Go Programming (O’Reilly)


Free Books on the Web:

Go by Example
https://gobyexample.com/

Learning Go
https://www.miek.nl/go/
in PDF: https://miek.nl/downloads/2015/go.pdf

An Introduction to Programming in Go
https://www.golang-book.com/books/intro
(Same author as Introducing Go. This was his first book.)