FAQ
Around to 2 days ago I was trying to make a character randomize. But I
couldn't get char variable, so this led to an
5 hours search for something this I'm was sure it existed. 4 hours later
I remembered that go dose not use ASCII char but it uses this uft8 char.
But any how I started using string after some more research I figer out
that string in Go is a array of runes not chars.

Now the questions is that is it a good idea to use string as a substitute
or is there a better way?

here is the code I was working on

func randChar() string {
op := rand.Intn(3)
var ans int
switch op{
case 0:
ans = '0' + rand.Intn(10) //integers
case 1:
ans = 'A' + rand.Intn(26) //uppercase
case 2:
ans = 'a' + rand.Intn(26) //lowercase
}
return string (ans)
}

//Bardia Jedi

--

Search Discussions

  • David DENG at Jan 24, 2013 at 4:08 pm
    Not sure about your trouble.

    String in go is a slice of bytes(uint8), and many build-in functions
    consider those bytes as utf8 encoding text. A string can be converted to a
    slice of rune, i.e. []rune.

    And randChar can be implemented in a simpler way:

    func randChar() byte {
    return
    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand.Intn(26+26+10)]
    }


    or http://play.golang.org/p/4Mul1X3NpR

    Hope any of the above helps.

    David

    On Thursday, January 24, 2013 6:32:18 PM UTC+8, Bardia Jedi wrote:

    Around to 2 days ago I was trying to make a character randomize. But I
    couldn't get char variable, so this led to an
    5 hours search for something this I'm was sure it existed. 4 hours later
    I remembered that go dose not use ASCII char but it uses this uft8 char.
    But any how I started using string after some more research I figer out
    that string in Go is a array of runes not chars.

    Now the questions is that is it a good idea to use string as a substitute
    or is there a better way?

    here is the code I was working on

    func randChar() string {
    op := rand.Intn(3)
    var ans int
    switch op{
    case 0:
    ans = '0' + rand.Intn(10) //integers
    case 1:
    ans = 'A' + rand.Intn(26) //uppercase
    case 2:
    ans = 'a' + rand.Intn(26) //lowercase
    }
    return string (ans)
    }

    //Bardia Jedi
    --
  • Itmitica at Jan 24, 2013 at 4:25 pm
    Obviously, randChar()is not working for utf8:
    http://play.golang.org/p/YuXUgPLJgd

    --
  • Itmitica at Jan 24, 2013 at 4:33 pm
    To be exact, strings in Go are slices of bytes. But the catch is, there
    isn't a 1on1 byte per character match.

    A char, or rune, in Go is a int32 literal, while byte is uint8. That means
    a utf8 string will usually have more bytes than runes:
    http://play.golang.org/p/adAqyin7P9

    --
  • Itmitica at Jan 24, 2013 at 5:17 pm
    I've made a more elaborate demo to illustrate the relations between bytes
    and runes and strings:

    http://play.golang.org/p/qUT3DH6U1Y

    --
  • Bardia Jedi at Jan 24, 2013 at 6:10 pm
    So if I understand it right it should look like this:

    string -> is an array of runes == "int32"
    runes -> is an array of bytes == ASCII

    so if u have the value of '^' and 'a' you can make this 'â'
    and rune of 'â' is []byte{ '^' , 'a' }.

    then this -> "är du" -> rune {[ ¨, a], r , d, u} right??



    On Thursday, January 24, 2013 11:32:18 AM UTC+1, Bardia Jedi wrote:

    Around to 2 days ago I was trying to make a character randomize. But I
    couldn't get char variable, so this led to an
    5 hours search for something this I'm was sure it existed. 4 hours later
    I remembered that go dose not use ASCII char but it uses this uft8 char.
    But any how I started using string after some more research I figer out
    that string in Go is a array of runes not chars.

    Now the questions is that is it a good idea to use string as a substitute
    or is there a better way?

    here is the code I was working on

    func randChar() string {
    op := rand.Intn(3)
    var ans int
    switch op{
    case 0:
    ans = '0' + rand.Intn(10) //integers
    case 1:
    ans = 'A' + rand.Intn(26) //uppercase
    case 2:
    ans = 'a' + rand.Intn(26) //lowercase
    }
    return string (ans)
    }

    //Bardia Jedi
    --
  • Minux at Jan 24, 2013 at 6:25 pm

    On Fri, Jan 25, 2013 at 2:10 AM, Bardia Jedi wrote:

    So if I understand it right it should look like this:

    string -> is an array of runes == "int32"
    string is represented as a slice of byte (uint8) [without the cap], but
    string definitely is not array.

    normally, Go strings are encoded as utf-8, but that's not required.

    however, if it's, Go provides some functionality to range over runes
    in it and convert them from/into a slice of rune, etc.

    runes -> is an array of bytes == ASCII
    >
    rune is an alias int32, and represent a unicode code point.

    in loose terms, a unicode code point is what you'd call a character, and
    by default, Go stores these "character values" as Go string encoded in a
    variable length encoding named utf-8. (you might want to read:
    http://en.wikipedia.org/wiki/Unicode and
    http://en.wikipedia.org/wiki/UTF-8
    )
    so if u have the value of '^' and 'a' you can make this 'â'
    and rune of 'â' is []byte{ '^' , 'a' }.
    no. see above for references.
    then this -> "är du" -> rune {[ ¨, a], r , d, u} right??
    seems you are talking about combining diacritical marks.
    they are quite different beasts, and strings in Go doesn't combine them
    as you've expected.

    --
  • Itmitica at Jan 24, 2013 at 6:41 pm
    The ASCII-UTF-8 is a bit of a long discussion. In short:

    A byte is a 8bit number.

    A UTF-8 char means a variable number of bytes, 1 byte up to four bytes.
    This means that a UTF-8 string like this "șț" is, in fact, an interpreted
    stream of bytes.

    Why variable number of bytes per char? The transformation is smart enough
    not to always use 4 bytes per char. It assigns as many bytes there are
    needed, a variable amount, so as to not waste 3 bytes for a 1 byte char.

    This stream of bytes, this encoding of variable byte size chars, has a the
    perfect Go equivalent: a slice. A slice is a variable size type. On the
    other hand, an array in Go is a fixed sized type: [4]byte, [2]byte.

    So a UTF-8 string in Go is perfectly reflecting the UTF-8 mechanism, an
    interpreted slice of variable size bytes: []byte.



    TL;DR
    You need more than 1 byte to construct a UTF-8 character (up to 4 bytes).
    Which, incidentally, is the rune type in Go: int32.

    But UTF-8 chars have variable byte size. Slices are variable in size. The
    bytes in them respect the UTF-8 encoding for byte transformation in chars
    (runes).

    --
  • Sonia Hamilton at Jan 25, 2013 at 12:34 am

    * Bardia Jedi [2013-01-24 10:10:27 -0800]:

    So if I understand it right it should look like this:

    string -> is an array of runes == "int32"
    runes -> is an array of bytes == ASCII

    so if u have the value of '^' and 'a' you can make this 'â'
    and rune of 'â' is []byte{ '^' , 'a' }.

    then this -> "är du" -> rune {[ ¨, a], r , d, u} right??
    Probably helps to get your head around UTF8 first [1], then Chapter 3 of
    [2] has a good exploration of Go's implementation.

    [1] http://www.joelonsoftware.com/articles/Unicode.html
    [2] http://www.qtrac.eu/gobook.html

    --
    Sonia Hamilton
    http://www.snowfrog.net
  • Bardia Jedi at Jan 24, 2013 at 6:43 pm
    Tack you all it help allot and i think that is have a grasp on the subject
    to a level of good understanding.

    But I'm still wandering if there is a way of combing bytes in to runes??

    //w/ thx BJ
    On Thursday, January 24, 2013 11:32:18 AM UTC+1, Bardia Jedi wrote:

    Around to 2 days ago I was trying to make a character randomize. But I
    couldn't get char variable, so this led to an
    5 hours search for something this I'm was sure it existed. 4 hours later
    I remembered that go dose not use ASCII char but it uses this uft8 char.
    But any how I started using string after some more research I figer out
    that string in Go is a array of runes not chars.

    Now the questions is that is it a good idea to use string as a substitute
    or is there a better way?

    here is the code I was working on

    func randChar() string {
    op := rand.Intn(3)
    var ans int
    switch op{
    case 0:
    ans = '0' + rand.Intn(10) //integers
    case 1:
    ans = 'A' + rand.Intn(26) //uppercase
    case 2:
    ans = 'a' + rand.Intn(26) //lowercase
    }
    return string (ans)
    }

    //Bardia Jedi
    --
  • Minux at Jan 24, 2013 at 6:59 pm

    On Fri, Jan 25, 2013 at 2:43 AM, Bardia Jedi wrote:

    But I'm still wandering if there is a way of combing bytes in to runes??
    can you give an example what do you want to do?

    --
  • Itmitica at Jan 24, 2013 at 7:01 pm
    Yes... but I'm afraid it's not the outcome you've expecting:
    http://play.golang.org/p/EOSh_Aef_K

    byte (uint8) and rune(int32) are numbers.
    An *interpreted* rune (int32) value can be a char:
    http://play.golang.org/p/wBhcIZ3LQK
    An *interpreted* stream of numbers can be a string:
    http://play.golang.org/p/HFEI75jdpq

    --
  • Bardia Jedi at Jan 24, 2013 at 7:13 pm
    no wandering it does work!!

    that's because your add the bytes together utf 8 is a table no
    a mathematics allgoertem what ur doing is accssing the position of (eg.)
    'a'+'b'
    eg. if we say that a -> 1 and b -> 2 by doing the a+b your getting c that
    is in position 3.

    what is was thinkin is more around the line of

    var r1 rune = string ([]byte {1 , 2 })



    On Thursday, January 24, 2013 8:01:48 PM UTC+1, itmi...@gmail.com wrote:

    Yes... but I'm afraid it's not the outcome you've expecting:
    http://play.golang.org/p/EOSh_Aef_K

    byte (uint8) and rune(int32) are numbers.
    An *interpreted* rune (int32) value can be a char:
    http://play.golang.org/p/wBhcIZ3LQK
    An *interpreted* stream of numbers can be a string:
    http://play.golang.org/p/HFEI75jdpq
    --
  • Itmitica at Jan 24, 2013 at 7:24 pm
    rune and string are very different.

    To combine two bytes to get a rune:
    http://play.golang.org/p/Ord10p_9hQ

    To combine two bytes to get a string:
    http://play.golang.org/p/7B9Q2JSaFu

    --
  • Itmitica at Jan 24, 2013 at 7:33 pm

    On Thursday, January 24, 2013 9:24:26 PM UTC+2, itmi...@gmail.com wrote:

    To combine two bytes to get a string:
    http://play.golang.org/p/7B9Q2JSaFu
    http://play.golang.org/p/lRoaRoTuMT

    --
  • Itmitica at Jan 24, 2013 at 7:32 pm
    And this is how you go from a string to a rune in it, if you know the byte
    order and encoding: http://play.golang.org/p/XG2J83ldmr

    Otherwise, you use the Rune related Go packages: utf8, bytes, strings.

    --
  • Bardia Jedi at Jan 24, 2013 at 7:50 pm
    ahw! So there is package

    tanks Mate very use full!
    On Thursday, January 24, 2013 8:32:35 PM UTC+1, itmi...@gmail.com wrote:

    And this is how you go from a string to a rune in it, if you know the byte
    order and encoding: http://play.golang.org/p/XG2J83ldmr

    Otherwise, you use the Rune related Go packages: utf8, bytes, strings.
    --
  • Itmitica at Jan 24, 2013 at 7:49 pm
    :)
    Yes, there are, right here: http://golang.org/pkg/

    --
  • Bryanturley at Jan 24, 2013 at 7:55 pm
    Is it safe to say that the Jedi Council approves of go then?


    --
  • Bardia Jedi at Feb 4, 2013 at 3:04 pm
    dude becuerful Jedi has been my family name for 3 or 4 genration
    On Thursday, January 24, 2013 8:55:55 PM UTC+1, bryanturley wrote:

    Is it safe to say that the Jedi Council approves of go then?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nate Finch at Jan 25, 2013 at 7:28 am
    One quick thing that hasn't been explicitly said (other than via links) is
    that UTF8 actually includes all of ASCII. Anything that is valid ASCII is
    valid UTF8, UTF8 just also includes a whole lot more. So if you know your
    code is only ever going to deal with ascii, you can use the individual
    bytes of a string as if they're C style characters.... but that's not
    always safe to do.
    On Thursday, January 24, 2013 5:32:18 AM UTC-5, Bardia Jedi wrote:

    Around to 2 days ago I was trying to make a character randomize. But I
    couldn't get char variable, so this led to an
    5 hours search for something this I'm was sure it existed. 4 hours later
    I remembered that go dose not use ASCII char but it uses this uft8 char.
    But any how I started using string after some more research I figer out
    that string in Go is a array of runes not chars.

    Now the questions is that is it a good idea to use string as a substitute
    or is there a better way?

    here is the code I was working on

    func randChar() string {
    op := rand.Intn(3)
    var ans int
    switch op{
    case 0:
    ans = '0' + rand.Intn(10) //integers
    case 1:
    ans = 'A' + rand.Intn(26) //uppercase
    case 2:
    ans = 'a' + rand.Intn(26) //lowercase
    }
    return string (ans)
    }

    //Bardia Jedi
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 24, '13 at 3:54p
activeFeb 4, '13 at 3:04p
posts21
users7
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase